Import of the watch repository from Pebble

This commit is contained in:
Matthieu Jeanson 2024-12-12 16:43:03 -08:00 committed by Katharine Berry
commit 3b92768480
10334 changed files with 2564465 additions and 0 deletions

View file

@ -0,0 +1,78 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js-parser-internal.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_bytecode Bytecode
* @{
*/
#define CBC_OPCODE(arg1, arg2, arg3, arg4) \
((arg2) | (((arg3) + CBC_STACK_ADJUST_BASE) << CBC_STACK_ADJUST_SHIFT)),
/**
* Flags of the opcodes.
*/
const uint8_t cbc_flags[] JERRY_CONST_DATA =
{
CBC_OPCODE_LIST
};
/**
* Flags of the extended opcodes.
*/
const uint8_t cbc_ext_flags[] =
{
CBC_EXT_OPCODE_LIST
};
#undef CBC_OPCODE
#ifdef PARSER_DUMP_BYTE_CODE
#define CBC_OPCODE(arg1, arg2, arg3, arg4) #arg1,
/**
* Names of the opcodes.
*/
const char * const cbc_names[] =
{
CBC_OPCODE_LIST
};
/**
* Names of the extended opcodes.
*/
const char * const cbc_ext_names[] =
{
CBC_EXT_OPCODE_LIST
};
#undef CBC_OPCODE
#endif /* PARSER_DUMP_BYTE_CODE */
/**
* @}
* @}
* @}
*/

View file

@ -0,0 +1,688 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef BYTE_CODE_H
#define BYTE_CODE_H
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_bytecode Bytecode
* @{
*/
/**
* Compact byte code (CBC) is a byte code representation
* of EcmaScript which is designed for low memory
* environments. Most opcodes are only one or sometimes
* two byte long so the CBC provides a small binary size.
*
* The execution engine of CBC is a stack machine, where
* the maximum stack size is known in advance for each
* function.
*/
/**
* Byte code flags. Only the lower 5 bit can be used
* since the stack change is encoded in the upper
* three bits for each instruction between -4 and 3
* (except for call / construct opcodes).
*/
#define CBC_STACK_ADJUST_BASE 4
#define CBC_STACK_ADJUST_SHIFT 5
#define CBC_STACK_ADJUST_VALUE(value) \
(((value) >> CBC_STACK_ADJUST_SHIFT) - CBC_STACK_ADJUST_BASE)
#define CBC_NO_FLAG 0x00u
#define CBC_HAS_LITERAL_ARG 0x01u
#define CBC_HAS_LITERAL_ARG2 0x02u
#define CBC_HAS_BYTE_ARG 0x04u
#define CBC_HAS_BRANCH_ARG 0x08u
/* These flags are shared */
#define CBC_FORWARD_BRANCH_ARG 0x10u
#define CBC_POP_STACK_BYTE_ARG 0x10u
#define CBC_ARG_TYPES (CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2 | CBC_HAS_BYTE_ARG | CBC_HAS_BRANCH_ARG)
#define CBC_HAS_POP_STACK_BYTE_ARG (CBC_HAS_BYTE_ARG | CBC_POP_STACK_BYTE_ARG)
/* Debug macro. */
#define CBC_ARGS_EQ(op, types) \
((cbc_flags[op] & CBC_ARG_TYPES) == (types))
/* Debug macro. */
#define CBC_SAME_ARGS(op1, op2) \
((cbc_flags[op1] & CBC_ARG_TYPES) == (cbc_flags[op2] & CBC_ARG_TYPES))
#define CBC_UNARY_OPERATION(name, group) \
CBC_OPCODE (name, CBC_NO_FLAG, 0, \
(VM_OC_ ## group) | VM_OC_GET_STACK | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _LITERAL, CBC_HAS_LITERAL_ARG, 1, \
(VM_OC_ ## group) | VM_OC_GET_LITERAL | VM_OC_PUT_STACK)
#define CBC_BINARY_OPERATION(name, group) \
CBC_OPCODE (name, CBC_NO_FLAG, -1, \
(VM_OC_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _RIGHT_LITERAL, CBC_HAS_LITERAL_ARG, 0, \
(VM_OC_ ## group) | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _TWO_LITERALS, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 1, \
(VM_OC_ ## group) | VM_OC_GET_LITERAL_LITERAL | VM_OC_PUT_STACK)
#define CBC_UNARY_LVALUE_OPERATION(name, group) \
CBC_OPCODE (name, CBC_NO_FLAG, -2, \
(VM_OC_PROP_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE) \
CBC_OPCODE (name ## _PUSH_RESULT, CBC_NO_FLAG, -1, \
(VM_OC_PROP_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _BLOCK, CBC_NO_FLAG, -2, \
(VM_OC_PROP_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
CBC_OPCODE (name ## _IDENT, CBC_HAS_LITERAL_ARG, 0, \
(VM_OC_ ## group) | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT) \
CBC_OPCODE (name ## _IDENT_PUSH_RESULT, CBC_HAS_LITERAL_ARG, 1, \
(VM_OC_ ## group) | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _IDENT_BLOCK, CBC_HAS_LITERAL_ARG, 0, \
(VM_OC_ ## group) | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT | VM_OC_PUT_BLOCK)
#define CBC_BINARY_LVALUE_OPERATION(name, group) \
CBC_OPCODE (name, CBC_NO_FLAG, -4, \
(VM_OC_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE) \
CBC_OPCODE (name ## _LITERAL, CBC_HAS_LITERAL_ARG, -3, \
(VM_OC_ ## group) | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_REFERENCE) \
#define CBC_EXT_BINARY_LVALUE_OPERATION(name, group) \
CBC_OPCODE (name ## _PUSH_RESULT, CBC_NO_FLAG, -3, \
(VM_OC_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (name ## _LITERAL_PUSH_RESULT, CBC_HAS_LITERAL_ARG, -2, \
(VM_OC_ ## group) | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
#define CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION(name, group) \
CBC_OPCODE (name ## _BLOCK, CBC_NO_FLAG, -4, \
(VM_OC_ ## group) | VM_OC_GET_STACK_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
CBC_OPCODE (name ## _LITERAL_BLOCK, CBC_HAS_LITERAL_ARG, -3, \
(VM_OC_ ## group) | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
#define CBC_UNARY_LVALUE_WITH_IDENT 3
#define CBC_BINARY_LVALUE_WITH_LITERAL 1
#define CBC_BINARY_WITH_LITERAL 1
#define CBC_BINARY_WITH_TWO_LITERALS 2
/**
* Several opcodes (mostly call and assignment opcodes) have
* two forms: one which does not push a return value onto
* the stack, and another which does. The reasion is that
* the return value of these opcodes are often not used
* and the first form provides smaller byte code.
*
* The following rules must be kept by the code generator:
* - only the opcode without return value can be emitted
* by the code generator
* - the first form can be converted to the second form
* by adding 1 to the opcode
* - after the conversion the opcode must be immediately
* flushed, so no further changes are possible
*
* Hence CBC_NO_RESULT_OPERATION (context_p->last_cbc_opcode)
* cannot be true for an opcode which has a result
*/
#define CBC_NO_RESULT_OPERATION(opcode) \
((opcode) >= CBC_PRE_INCR && (opcode) < CBC_END)
#define CBC_NO_RESULT_BLOCK(opcode) \
((opcode) >= CBC_PRE_INCR && (opcode) < CBC_ASSIGN_ADD)
#define CBC_NO_RESULT_COMPOUND_ASSIGMENT(opcode) \
((opcode) >= CBC_ASSIGN_ADD && (opcode) < CBC_END)
/**
* Branch instructions are organized in group of 8 opcodes.
* - 1st opcode: unused, can be used for other purpose
* - 2nd opcode: forward branch with 1 byte offset
* - 3rd opcode: forward branch with 2 byte offset
* - 4th opcode: forward branch with 3 byte offset
* - 5th opcode: unused, can be used for other purpose
* - 6th opcode: backward branch with 1 byte offset
* - 7th opcode: backward branch with 2 byte offset
* - 8th opcode: backward branch with 3 byte offset
*
* Reasons:
* The branch_opcode & 0x3 tells the length in bytes of the offset
* If branch offset & 0x4 == 0, it is a forward branch. Otherwise
* it is backward.
*
* The offset bytes are encoded in higher to lower order.
*/
#define CBC_FORWARD_BRANCH(name, stack, vm_oc) \
CBC_OPCODE (name, CBC_HAS_BRANCH_ARG | CBC_FORWARD_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH) \
CBC_OPCODE (name ## _2, CBC_HAS_BRANCH_ARG | CBC_FORWARD_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH) \
CBC_OPCODE (name ## _3, CBC_HAS_BRANCH_ARG | CBC_FORWARD_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH)
#define CBC_BACKWARD_BRANCH(name, stack, vm_oc) \
CBC_OPCODE (name, CBC_HAS_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH | VM_OC_BACKWARD_BRANCH) \
CBC_OPCODE (name ## _2, CBC_HAS_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH | VM_OC_BACKWARD_BRANCH) \
CBC_OPCODE (name ## _3, CBC_HAS_BRANCH_ARG, stack, \
(vm_oc) | VM_OC_GET_BRANCH | VM_OC_BACKWARD_BRANCH)
#define CBC_BRANCH_OFFSET_LENGTH(opcode) \
((opcode) & 0x3)
#define CBC_BRANCH_IS_BACKWARD(flags) \
(!((flags) & CBC_FORWARD_BRANCH_ARG))
#define CBC_BRANCH_IS_FORWARD(flags) \
((flags) & CBC_FORWARD_BRANCH_ARG)
/* Stack consumption of opcodes with context. */
/* PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION must be <= 4 */
#define PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION 3
/* PARSER_WITH_CONTEXT_STACK_ALLOCATION must be <= 4 */
#define PARSER_WITH_CONTEXT_STACK_ALLOCATION 2
/* PARSER_TRY_CONTEXT_STACK_ALLOCATION must be <= 3 */
#define PARSER_TRY_CONTEXT_STACK_ALLOCATION 2
/**
* Opcode definitions.
*/
#define CBC_OPCODE_LIST \
/* Branch opcodes first. Some other opcodes are mixed. */ \
CBC_OPCODE (CBC_EXT_OPCODE, CBC_NO_FLAG, 0, \
VM_OC_NONE) \
CBC_FORWARD_BRANCH (CBC_JUMP_FORWARD, 0, \
VM_OC_JUMP) \
CBC_OPCODE (CBC_POP, CBC_NO_FLAG, -1, \
VM_OC_POP) \
CBC_BACKWARD_BRANCH (CBC_JUMP_BACKWARD, 0, \
VM_OC_JUMP) \
CBC_OPCODE (CBC_POP_BLOCK, CBC_NO_FLAG, -1, \
VM_OC_POP_BLOCK | VM_OC_PUT_BLOCK) \
CBC_FORWARD_BRANCH (CBC_BRANCH_IF_TRUE_FORWARD, -1, \
VM_OC_BRANCH_IF_TRUE) \
CBC_OPCODE (CBC_THROW, CBC_NO_FLAG, -1, \
VM_OC_THROW | VM_OC_GET_STACK) \
CBC_BACKWARD_BRANCH (CBC_BRANCH_IF_TRUE_BACKWARD, -1, \
VM_OC_BRANCH_IF_TRUE) \
CBC_OPCODE (CBC_CONTEXT_END, CBC_NO_FLAG, 0, \
VM_OC_CONTEXT_END) \
CBC_FORWARD_BRANCH (CBC_BRANCH_IF_FALSE_FORWARD, -1, \
VM_OC_BRANCH_IF_FALSE) \
CBC_OPCODE (CBC_CREATE_OBJECT, CBC_NO_FLAG, 1, \
VM_OC_PUSH_OBJECT | VM_OC_PUT_STACK) \
CBC_BACKWARD_BRANCH (CBC_BRANCH_IF_FALSE_BACKWARD, -1, \
VM_OC_BRANCH_IF_FALSE) \
CBC_OPCODE (CBC_SET_PROPERTY, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_SET_PROPERTY | VM_OC_GET_STACK_LITERAL) \
CBC_FORWARD_BRANCH (CBC_JUMP_FORWARD_EXIT_CONTEXT, 0, \
VM_OC_JUMP_AND_EXIT_CONTEXT) \
CBC_OPCODE (CBC_CREATE_ARRAY, CBC_NO_FLAG, 1, \
VM_OC_PUSH_ARRAY | VM_OC_PUT_STACK) \
CBC_FORWARD_BRANCH (CBC_BRANCH_IF_LOGICAL_TRUE, -1, \
VM_OC_BRANCH_IF_LOGICAL_TRUE) \
CBC_OPCODE (CBC_ARRAY_APPEND, CBC_HAS_POP_STACK_BYTE_ARG, 0, \
VM_OC_APPEND_ARRAY) \
CBC_FORWARD_BRANCH (CBC_BRANCH_IF_LOGICAL_FALSE, -1, \
VM_OC_BRANCH_IF_LOGICAL_FALSE) \
CBC_OPCODE (CBC_PUSH_ELISION, CBC_NO_FLAG, 1, \
VM_OC_PUSH_ELISON | VM_OC_PUT_STACK) \
CBC_FORWARD_BRANCH (CBC_BRANCH_IF_STRICT_EQUAL, -1, \
VM_OC_BRANCH_IF_STRICT_EQUAL) \
\
/* Basic opcodes. */ \
CBC_OPCODE (CBC_PUSH_LITERAL, CBC_HAS_LITERAL_ARG, 1, \
VM_OC_PUSH | VM_OC_GET_LITERAL) \
CBC_OPCODE (CBC_PUSH_TWO_LITERALS, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 2, \
VM_OC_PUSH_TWO | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_PUSH_THREE_LITERALS, CBC_HAS_LITERAL_ARG2, 3, \
VM_OC_PUSH_THREE | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_PUSH_UNDEFINED, CBC_NO_FLAG, 1, \
VM_OC_PUSH_UNDEFINED | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_TRUE, CBC_NO_FLAG, 1, \
VM_OC_PUSH_TRUE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_FALSE, CBC_NO_FLAG, 1, \
VM_OC_PUSH_FALSE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_NULL, CBC_NO_FLAG, 1, \
VM_OC_PUSH_NULL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_THIS, CBC_NO_FLAG, 1, \
VM_OC_PUSH_THIS | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_THIS_LITERAL, CBC_HAS_LITERAL_ARG, 2, \
VM_OC_PUSH_TWO | VM_OC_GET_THIS_LITERAL) \
CBC_OPCODE (CBC_PUSH_NUMBER_0, CBC_NO_FLAG, 1, \
VM_OC_PUSH_NUMBER_0 | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_NUMBER_POS_BYTE, CBC_HAS_BYTE_ARG, 1, \
VM_OC_PUSH_NUMBER_POS_BYTE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_NUMBER_NEG_BYTE, CBC_HAS_BYTE_ARG, 1, \
VM_OC_PUSH_NUMBER_NEG_BYTE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP, CBC_NO_FLAG, -1, \
VM_OC_PROP_GET | VM_OC_GET_STACK_STACK | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_LITERAL, CBC_HAS_LITERAL_ARG, 0, \
VM_OC_PROP_GET | VM_OC_GET_STACK_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_LITERAL_LITERAL, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 1, \
VM_OC_PROP_GET | VM_OC_GET_LITERAL_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_THIS_LITERAL, CBC_HAS_LITERAL_ARG, 1, \
VM_OC_PROP_GET | VM_OC_GET_THIS_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_IDENT_REFERENCE, CBC_HAS_LITERAL_ARG, 3, \
VM_OC_IDENT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_REFERENCE, CBC_NO_FLAG, 1, \
VM_OC_PROP_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_LITERAL_REFERENCE, CBC_HAS_LITERAL_ARG, 2, \
VM_OC_PROP_REFERENCE | VM_OC_GET_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_LITERAL_LITERAL_REFERENCE, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 3, \
VM_OC_PROP_REFERENCE | VM_OC_GET_LITERAL_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_PUSH_PROP_THIS_LITERAL_REFERENCE, CBC_HAS_LITERAL_ARG, 3, \
VM_OC_PROP_REFERENCE | VM_OC_GET_THIS_LITERAL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_NEW, CBC_HAS_POP_STACK_BYTE_ARG, 0, \
VM_OC_NEW | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_NEW0, CBC_NO_FLAG, 0, \
VM_OC_NEW | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_NEW1, CBC_NO_FLAG, -1, \
VM_OC_NEW | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_EVAL, CBC_NO_FLAG, 0, \
VM_OC_EVAL) \
CBC_OPCODE (CBC_DEFINE_VARS, CBC_HAS_LITERAL_ARG, 0, \
VM_OC_NONE) \
CBC_OPCODE (CBC_INITIALIZE_VAR, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_NONE) \
CBC_OPCODE (CBC_INITIALIZE_VARS, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_NONE) \
CBC_OPCODE (CBC_SET_BYTECODE_PTR, CBC_NO_FLAG, 0, \
VM_OC_NONE) \
CBC_OPCODE (CBC_RETURN, CBC_NO_FLAG, -1, \
VM_OC_RET | VM_OC_GET_STACK) \
CBC_OPCODE (CBC_RETURN_WITH_BLOCK, CBC_NO_FLAG, 0, \
VM_OC_RET) \
CBC_OPCODE (CBC_RETURN_WITH_LITERAL, CBC_HAS_LITERAL_ARG, 0, \
VM_OC_RET | VM_OC_GET_LITERAL) \
\
/* Unary opcodes. */ \
CBC_UNARY_OPERATION (CBC_PLUS, \
PLUS) \
CBC_UNARY_OPERATION (CBC_NEGATE, \
MINUS) \
CBC_UNARY_OPERATION (CBC_LOGICAL_NOT, \
NOT) \
CBC_UNARY_OPERATION (CBC_BIT_NOT, \
BIT_NOT) \
CBC_UNARY_OPERATION (CBC_VOID, \
VOID) \
CBC_OPCODE (CBC_TYPEOF, CBC_NO_FLAG, 0, \
VM_OC_TYPEOF | VM_OC_GET_STACK | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_TYPEOF_IDENT, CBC_HAS_LITERAL_ARG, 1, \
VM_OC_TYPEOF_IDENT | VM_OC_PUT_STACK) \
\
/* Binary opcodes. */ \
CBC_BINARY_OPERATION (CBC_BIT_OR, \
BIT_OR) \
CBC_BINARY_OPERATION (CBC_BIT_XOR, \
BIT_XOR) \
CBC_BINARY_OPERATION (CBC_BIT_AND, \
BIT_AND) \
CBC_BINARY_OPERATION (CBC_EQUAL, \
EQUAL) \
CBC_BINARY_OPERATION (CBC_NOT_EQUAL, \
NOT_EQUAL) \
CBC_BINARY_OPERATION (CBC_STRICT_EQUAL, \
STRICT_EQUAL) \
CBC_BINARY_OPERATION (CBC_STRICT_NOT_EQUAL, \
STRICT_NOT_EQUAL) \
CBC_BINARY_OPERATION (CBC_LESS, \
LESS) \
CBC_BINARY_OPERATION (CBC_GREATER, \
GREATER) \
CBC_BINARY_OPERATION (CBC_LESS_EQUAL, \
LESS_EQUAL) \
CBC_BINARY_OPERATION (CBC_GREATER_EQUAL, \
GREATER_EQUAL) \
CBC_BINARY_OPERATION (CBC_IN, \
IN) \
CBC_BINARY_OPERATION (CBC_INSTANCEOF, \
INSTANCEOF) \
CBC_BINARY_OPERATION (CBC_LEFT_SHIFT, \
LEFT_SHIFT) \
CBC_BINARY_OPERATION (CBC_RIGHT_SHIFT, \
RIGHT_SHIFT) \
CBC_BINARY_OPERATION (CBC_UNS_RIGHT_SHIFT, \
UNS_RIGHT_SHIFT) \
CBC_BINARY_OPERATION (CBC_ADD, \
ADD) \
CBC_BINARY_OPERATION (CBC_SUBTRACT, \
SUB) \
CBC_BINARY_OPERATION (CBC_MULTIPLY, \
MUL) \
CBC_BINARY_OPERATION (CBC_DIVIDE, \
DIV) \
CBC_BINARY_OPERATION (CBC_MODULO, \
MOD) \
\
/* Unary lvalue opcodes. */ \
CBC_OPCODE (CBC_DELETE_PUSH_RESULT, CBC_NO_FLAG, -1, \
VM_OC_PROP_DELETE | VM_OC_GET_STACK_STACK | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_DELETE_IDENT_PUSH_RESULT, CBC_HAS_LITERAL_ARG, 1, \
VM_OC_DELETE | VM_OC_PUT_STACK) \
CBC_UNARY_LVALUE_OPERATION (CBC_PRE_INCR, \
PRE_INCR) \
CBC_UNARY_LVALUE_OPERATION (CBC_PRE_DECR, \
PRE_DECR) \
CBC_UNARY_LVALUE_OPERATION (CBC_POST_INCR, \
POST_INCR) \
CBC_UNARY_LVALUE_OPERATION (CBC_POST_DECR, \
POST_DECR) \
\
/* Call opcodes. */ \
CBC_OPCODE (CBC_CALL, CBC_HAS_POP_STACK_BYTE_ARG, -1, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL_PUSH_RESULT, CBC_HAS_POP_STACK_BYTE_ARG, 0, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL_BLOCK, CBC_HAS_POP_STACK_BYTE_ARG, -1, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL_PROP, CBC_HAS_POP_STACK_BYTE_ARG, -3, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL_PROP_PUSH_RESULT, CBC_HAS_POP_STACK_BYTE_ARG, -2, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL_PROP_BLOCK, CBC_HAS_POP_STACK_BYTE_ARG, -3, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL0, CBC_NO_FLAG, -1, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL0_PUSH_RESULT, CBC_NO_FLAG, 0, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL0_BLOCK, CBC_NO_FLAG, -1, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL0_PROP, CBC_NO_FLAG, -3, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL0_PROP_PUSH_RESULT, CBC_NO_FLAG, -2, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL0_PROP_BLOCK, CBC_NO_FLAG, -3, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL1, CBC_NO_FLAG, -2, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL1_PUSH_RESULT, CBC_NO_FLAG, -1, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL1_BLOCK, CBC_NO_FLAG, -2, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL1_PROP, CBC_NO_FLAG, -4, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL1_PROP_PUSH_RESULT, CBC_NO_FLAG, -3, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL1_PROP_BLOCK, CBC_NO_FLAG, -4, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL2, CBC_NO_FLAG, -3, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL2_PUSH_RESULT, CBC_NO_FLAG, -2, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL2_BLOCK, CBC_NO_FLAG, -3, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_CALL2_PROP, CBC_NO_FLAG, -4, \
VM_OC_CALL) \
CBC_OPCODE (CBC_CALL2_PROP_PUSH_RESULT, CBC_NO_FLAG, -3, \
VM_OC_CALL | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_CALL2_PROP_BLOCK, CBC_NO_FLAG, -4, \
VM_OC_CALL | VM_OC_PUT_BLOCK) \
\
/* Binary assignment opcodes. */ \
CBC_OPCODE (CBC_ASSIGN, CBC_NO_FLAG, -3, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_REFERENCE) \
CBC_OPCODE (CBC_ASSIGN_PUSH_RESULT, CBC_NO_FLAG, -2, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_ASSIGN_BLOCK, CBC_NO_FLAG, -3, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_ASSIGN_SET_IDENT, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_IDENT) \
CBC_OPCODE (CBC_ASSIGN_SET_IDENT_PUSH_RESULT, CBC_HAS_LITERAL_ARG, 0, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_IDENT | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_ASSIGN_SET_IDENT_BLOCK, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_ASSIGN | VM_OC_GET_STACK | VM_OC_PUT_IDENT | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_ASSIGN_LITERAL_SET_IDENT, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_ASSIGN | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT) \
CBC_OPCODE (CBC_ASSIGN_LITERAL_SET_IDENT_PUSH_RESULT, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 1, \
VM_OC_ASSIGN | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_ASSIGN_LITERAL_SET_IDENT_BLOCK, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_ASSIGN | VM_OC_GET_LITERAL | VM_OC_PUT_IDENT | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_ASSIGN_PROP_LITERAL, CBC_HAS_LITERAL_ARG, -2, \
VM_OC_ASSIGN_PROP | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE) \
CBC_OPCODE (CBC_ASSIGN_PROP_LITERAL_PUSH_RESULT, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_ASSIGN_PROP | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_ASSIGN_PROP_LITERAL_BLOCK, CBC_HAS_LITERAL_ARG, -2, \
VM_OC_ASSIGN_PROP | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
CBC_OPCODE (CBC_ASSIGN_PROP_THIS_LITERAL, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_ASSIGN_PROP_THIS | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE) \
CBC_OPCODE (CBC_ASSIGN_PROP_THIS_LITERAL_PUSH_RESULT, CBC_HAS_LITERAL_ARG, 0, \
VM_OC_ASSIGN_PROP_THIS | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_STACK) \
CBC_OPCODE (CBC_ASSIGN_PROP_THIS_LITERAL_BLOCK, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_ASSIGN_PROP_THIS | VM_OC_GET_LITERAL | VM_OC_PUT_REFERENCE | VM_OC_PUT_BLOCK) \
\
/* Binary compound assignment opcodes. */ \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_ADD, \
ADD) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_SUBTRACT, \
SUB) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_MULTIPLY, \
MUL) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_DIVIDE, \
DIV) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_MODULO, \
MOD) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_LEFT_SHIFT, \
LEFT_SHIFT) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_RIGHT_SHIFT, \
RIGHT_SHIFT) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_UNS_RIGHT_SHIFT, \
UNS_RIGHT_SHIFT) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_BIT_AND, \
BIT_AND) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_BIT_OR, \
BIT_OR) \
CBC_BINARY_LVALUE_OPERATION (CBC_ASSIGN_BIT_XOR, \
BIT_XOR) \
\
/* Last opcode (not a real opcode). */ \
CBC_OPCODE (CBC_END, CBC_NO_FLAG, 0, \
VM_OC_NONE)
/* All EXT branches are statement block end
* marks, so they are always forward branches. */
#define CBC_EXT_OPCODE_LIST \
/* Branch opcodes first. Some other opcodes are mixed. */ \
CBC_OPCODE (CBC_EXT_NOP, CBC_NO_FLAG, 0, \
VM_OC_NONE) \
CBC_FORWARD_BRANCH (CBC_EXT_WITH_CREATE_CONTEXT, \
-1 + PARSER_WITH_CONTEXT_STACK_ALLOCATION, VM_OC_WITH) \
CBC_OPCODE (CBC_EXT_FOR_IN_GET_NEXT, CBC_NO_FLAG, 1, \
VM_OC_FOR_IN_GET_NEXT | VM_OC_PUT_STACK) \
CBC_FORWARD_BRANCH (CBC_EXT_FOR_IN_CREATE_CONTEXT, \
-1 + PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION, VM_OC_FOR_IN_CREATE_CONTEXT) \
CBC_OPCODE (CBC_EXT_SET_GETTER, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_GETTER | VM_OC_GET_LITERAL_LITERAL) \
CBC_BACKWARD_BRANCH (CBC_EXT_BRANCH_IF_FOR_IN_HAS_NEXT, 0, \
VM_OC_FOR_IN_HAS_NEXT) \
CBC_OPCODE (CBC_EXT_SET_SETTER, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_SETTER | VM_OC_GET_LITERAL_LITERAL) \
CBC_FORWARD_BRANCH (CBC_EXT_TRY_CREATE_CONTEXT, PARSER_TRY_CONTEXT_STACK_ALLOCATION, \
VM_OC_TRY) \
CBC_OPCODE (CBC_EXT_THROW_REFERENCE_ERROR, CBC_NO_FLAG, 1, \
VM_OC_THROW_REFERENCE_ERROR) \
CBC_FORWARD_BRANCH (CBC_EXT_CATCH, 1, \
VM_OC_CATCH) \
CBC_OPCODE (CBC_EXT_PUSH_UNDEFINED_BASE, CBC_NO_FLAG, 1, \
VM_OC_PUSH_UNDEFINED_BASE | VM_OC_PUT_STACK) \
CBC_FORWARD_BRANCH (CBC_EXT_FINALLY, 0, \
VM_OC_FINALLY) \
\
/* Basic opcodes. */ \
CBC_OPCODE (CBC_EXT_DEBUGGER, CBC_NO_FLAG, 0, \
VM_OC_NONE) \
\
/* Binary compound assignment opcodes with pushing the result. */ \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_ADD, \
ADD) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_SUBTRACT, \
SUB) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_MULTIPLY, \
MUL) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_DIVIDE, \
DIV) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_MODULO, \
MOD) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_LEFT_SHIFT, \
LEFT_SHIFT) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_RIGHT_SHIFT, \
RIGHT_SHIFT) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_UNS_RIGHT_SHIFT, \
UNS_RIGHT_SHIFT) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_BIT_AND, \
BIT_AND) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_BIT_OR, \
BIT_OR) \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_BIT_XOR, \
BIT_XOR) \
\
/* Binary compound assignment opcodes with saving the result. */ \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_ADD, \
ADD) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_SUBTRACT, \
SUB) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_MULTIPLY, \
MUL) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_DIVIDE, \
DIV) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_MODULO, \
MOD) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_LEFT_SHIFT, \
LEFT_SHIFT) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_RIGHT_SHIFT, \
RIGHT_SHIFT) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_UNS_RIGHT_SHIFT, \
UNS_RIGHT_SHIFT) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_BIT_AND, \
BIT_AND) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_BIT_OR, \
BIT_OR) \
CBC_EXT_BINARY_LVALUE_BLOCK_OPERATION (CBC_EXT_ASSIGN_BIT_XOR, \
BIT_XOR) \
\
/* Last opcode (not a real opcode). */ \
CBC_OPCODE (CBC_EXT_END, CBC_NO_FLAG, 0, \
VM_OC_NONE)
#define CBC_MAXIMUM_BYTE_VALUE 255
#define CBC_MAXIMUM_SMALL_VALUE 510
#define CBC_MAXIMUM_FULL_VALUE 32767
#define CBC_PUSH_NUMBER_BYTE_RANGE_END 256
#define CBC_HIGHEST_BIT_MASK 0x80
#define CBC_LOWER_SEVEN_BIT_MASK 0x7f
/**
* Literal indicies belong to one of the following groups:
*
* 0 <= index < argument_end : arguments
* argument_end <= index < register_end : registers
* register_end <= index < ident_end : identifiers
* ident_end <= index < const_literal_end : constant literals
* const_literal_end <= index < literal_end : template literals
*/
/**
* Compiled byte code arguments.
*/
typedef struct
{
ecma_compiled_code_t header; /**< compiled code header */
uint8_t stack_limit; /**< maximum number of values stored on the stack */
uint8_t argument_end; /**< number of arguments expected by the function */
uint8_t register_end; /**< end position of the register group */
uint8_t ident_end; /**< end position of the identifier group */
uint8_t const_literal_end; /**< end position of the const literal group */
uint8_t literal_end; /**< end position of the literal group */
} cbc_uint8_arguments_t;
/**
* Compiled byte code arguments.
*/
typedef struct
{
ecma_compiled_code_t header; /**< compiled code header */
uint16_t stack_limit; /**< maximum number of values stored on the stack */
uint16_t argument_end; /**< number of arguments expected by the function */
uint16_t register_end; /**< end position of the register group */
uint16_t ident_end; /**< end position of the identifier group */
uint16_t const_literal_end; /**< end position of the const literal group */
uint16_t literal_end; /**< end position of the literal group */
} cbc_uint16_arguments_t;
/* When CBC_CODE_FLAGS_FULL_LITERAL_ENCODING
* is not set the small encoding is used. */
#define CBC_CODE_FLAGS_FUNCTION 0x01
#define CBC_CODE_FLAGS_FULL_LITERAL_ENCODING 0x02
#define CBC_CODE_FLAGS_UINT16_ARGUMENTS 0x04
#define CBC_CODE_FLAGS_STRICT_MODE 0x08
#define CBC_CODE_FLAGS_ARGUMENTS_NEEDED 0x10
#define CBC_CODE_FLAGS_LEXICAL_ENV_NOT_NEEDED 0x20
#define CBC_OPCODE(arg1, arg2, arg3, arg4) arg1,
/**
* Opcode list.
*/
typedef enum
{
CBC_OPCODE_LIST /**< list of opcodes */
} cbc_opcode_t;
/**
* Extended opcode list.
*/
typedef enum
{
CBC_EXT_OPCODE_LIST /**< list extended opcodes */
} cbc_ext_opcode_t;
#undef CBC_OPCODE
/**
* Opcode flags.
*/
extern const uint8_t cbc_flags[];
extern const uint8_t cbc_ext_flags[];
#ifdef PARSER_DUMP_BYTE_CODE
/**
* Opcode names for debugging.
*/
extern const char * const cbc_names[];
extern const char * const cbc_ext_names[];
#endif /* PARSER_DUMP_BYTE_CODE */
/**
* @}
* @}
* @}
*/
#endif /* !BYTE_CODE_H */

View file

@ -0,0 +1,136 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common.h"
#include "ecma-helpers.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_utils Utility
* @{
*/
/**
* Free literal.
*/
void
util_free_literal (lexer_literal_t *literal_p) /**< literal */
{
if (literal_p->type == LEXER_IDENT_LITERAL
|| literal_p->type == LEXER_STRING_LITERAL)
{
if (!(literal_p->status_flags & LEXER_FLAG_SOURCE_PTR))
{
jmem_heap_free_block ((void *) literal_p->u.char_p, literal_p->prop.length);
}
}
else if ((literal_p->type == LEXER_FUNCTION_LITERAL)
|| (literal_p->type == LEXER_REGEXP_LITERAL))
{
ecma_bytecode_deref (literal_p->u.bytecode_p);
}
} /* util_free_literal */
#ifdef PARSER_DUMP_BYTE_CODE
/**
* Debug utility to print a character sequence.
*/
static void
util_print_chars (const uint8_t *char_p, /**< character pointer */
size_t size) /**< size */
{
while (size > 0)
{
JERRY_DEBUG_MSG ("%c", *char_p++);
size--;
}
} /* util_print_chars */
/**
* Debug utility to print a number.
*/
static void
util_print_number (ecma_number_t num_p) /**< number to print */
{
lit_utf8_byte_t str_buf[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
lit_utf8_size_t str_size = ecma_number_to_utf8_string (num_p, str_buf, sizeof (str_buf));
str_buf[str_size] = 0;
JERRY_DEBUG_MSG ("%s", str_buf);
} /* util_print_number */
/**
* Print literal.
*/
void
util_print_literal (lexer_literal_t *literal_p) /**< literal */
{
if (literal_p->type == LEXER_IDENT_LITERAL)
{
if (literal_p->status_flags & LEXER_FLAG_VAR)
{
JERRY_DEBUG_MSG ("var_ident(");
}
else
{
JERRY_DEBUG_MSG ("ident(");
}
util_print_chars (literal_p->u.char_p, literal_p->prop.length);
}
else if (literal_p->type == LEXER_FUNCTION_LITERAL)
{
JERRY_DEBUG_MSG ("function");
return;
}
else if (literal_p->type == LEXER_STRING_LITERAL)
{
JERRY_DEBUG_MSG ("string(");
util_print_chars (literal_p->u.char_p, literal_p->prop.length);
}
else if (literal_p->type == LEXER_NUMBER_LITERAL)
{
ecma_string_t *value_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t, literal_p->u.value);
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (value_p) == ECMA_STRING_LITERAL_NUMBER);
JERRY_DEBUG_MSG ("number(");
util_print_number (ecma_get_number_from_value (value_p->u.lit_number));
}
else if (literal_p->type == LEXER_REGEXP_LITERAL)
{
JERRY_DEBUG_MSG ("regexp");
return;
}
else
{
JERRY_DEBUG_MSG ("unknown");
return;
}
JERRY_DEBUG_MSG (")");
} /* util_print_literal */
#endif /* PARSER_DUMP_BYTE_CODE */
/**
* @}
* @}
* @}
*/

View file

@ -0,0 +1,137 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef COMMON_H
#define COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <setjmp.h>
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_utils Utility
* @{
*/
#include "ecma-globals.h"
#include "ecma-regexp-object.h"
#include "jmem-heap.h"
/* Immediate management. */
/**
* Literal types.
*
* The LEXER_UNUSED_LITERAL type is internal and
* used for various purposes.
*/
typedef enum
{
LEXER_IDENT_LITERAL = 0, /**< identifier literal */
LEXER_STRING_LITERAL = 1, /**< string literal */
LEXER_NUMBER_LITERAL = 2, /**< number literal */
LEXER_FUNCTION_LITERAL = 3, /**< function literal */
LEXER_REGEXP_LITERAL = 4, /**< regexp literal */
LEXER_UNUSED_LITERAL = 5, /**< unused literal, can only be
used by the byte code generator. */
} lexer_literal_type_t;
/* Flags for status_flags. */
/* Local identifier (var, function arg). */
#define LEXER_FLAG_VAR 0x01
/* This local identifier cannot be stored in register. */
#define LEXER_FLAG_NO_REG_STORE 0x02
/* This local identifier is initialized with a value. */
#define LEXER_FLAG_INITIALIZED 0x04
/* This local identifier has a reference to the function itself. */
#define LEXER_FLAG_FUNCTION_NAME 0x08
/* This local identifier is a function argument. */
#define LEXER_FLAG_FUNCTION_ARGUMENT 0x10
/* No space is allocated for this character literal. */
#define LEXER_FLAG_SOURCE_PTR 0x20
/* Initialize this variable after the byte code is freed. */
#define LEXER_FLAG_LATE_INIT 0x40
/**
* Literal data.
*/
typedef struct
{
union
{
jmem_cpointer_t value; /**< literal value (not processed by the parser) */
const uint8_t *char_p; /**< character value */
ecma_compiled_code_t *bytecode_p; /**< compiled function or regexp pointer */
uint32_t source_data; /**< encoded source literal */
} u;
#ifdef PARSER_DUMP_BYTE_CODE
struct
#else /* !PARSER_DUMP_BYTE_CODE */
union
#endif /* PARSER_DUMP_BYTE_CODE */
{
uint16_t length; /**< length of ident / string literal */
uint16_t index; /**< real index during post processing */
} prop;
uint8_t type; /**< type of the literal */
uint8_t status_flags; /**< status flags */
} lexer_literal_t;
void util_free_literal (lexer_literal_t *);
#ifdef PARSER_DUMP_BYTE_CODE
void util_print_literal (lexer_literal_t *);
#endif /* PARSER_DUMP_BYTE_CODE */
/* TRY/CATCH block */
#define PARSER_TRY_CONTEXT(context_name) \
jmp_buf context_name
#define PARSER_THROW(context_name) \
longjmp (context_name, 1);
#define PARSER_TRY(context_name) \
{ \
if (!setjmp (context_name)) \
{ \
#define PARSER_CATCH \
} \
else \
{
#define PARSER_TRY_END \
} \
}
/**
* @}
* @}
* @}
*/
#endif /* !COMMON_H */

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,269 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef JS_LEXER_H
#define JS_LEXER_H
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_lexer Lexer
* @{
*/
/**
* Lexer token types.
*/
typedef enum
{
LEXER_EOS, /**< end of source */
/* Primary expressions */
LEXER_LITERAL, /**< literal token */
LEXER_KEYW_THIS, /**< this */
LEXER_LIT_TRUE, /**< true (not a keyword!) */
LEXER_LIT_FALSE, /**< false (not a keyword!) */
LEXER_LIT_NULL, /**< null (not a keyword!) */
/* Unary operators
* IMPORTANT: update CBC_UNARY_OP_TOKEN_TO_OPCODE and
* CBC_UNARY_LVALUE_OP_TOKEN_TO_OPCODE after changes. */
#define LEXER_IS_UNARY_OP_TOKEN(token_type) \
((token_type) >= LEXER_PLUS && (token_type) <= LEXER_DECREASE)
#define LEXER_IS_UNARY_LVALUE_OP_TOKEN(token_type) \
((token_type) >= LEXER_KEYW_DELETE && (token_type) <= LEXER_DECREASE)
LEXER_PLUS, /**< "+" */
LEXER_NEGATE, /**< "-" */
LEXER_LOGICAL_NOT, /**< "!" */
LEXER_BIT_NOT, /**< "~" */
LEXER_KEYW_VOID, /**< void */
LEXER_KEYW_TYPEOF, /**< typeof */
LEXER_KEYW_DELETE, /**< delete */
LEXER_INCREASE, /**< "++" */
LEXER_DECREASE, /**< "--" */
/* Binary operators
* IMPORTANT: update CBC_BINARY_OP_TOKEN_TO_OPCODE,
* CBC_BINARY_LVALUE_OP_TOKEN_TO_OPCODE and
* parser_binary_precedence_table after changes. */
#define LEXER_IS_BINARY_OP_TOKEN(token_type) \
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_MODULO)
#define LEXER_IS_BINARY_LVALUE_TOKEN(token_type) \
((token_type) >= LEXER_ASSIGN && (token_type) <= LEXER_ASSIGN_BIT_XOR)
#define LEXER_FIRST_BINARY_OP LEXER_ASSIGN
LEXER_ASSIGN, /**< "=" (prec: 3) */
LEXER_ASSIGN_ADD, /**< "+=" (prec: 3) */
LEXER_ASSIGN_SUBTRACT, /**< "-=" (prec: 3) */
LEXER_ASSIGN_MULTIPLY, /**< "*=" (prec: 3) */
LEXER_ASSIGN_DIVIDE, /**< "/=" (prec: 3) */
LEXER_ASSIGN_MODULO, /**< "%=" (prec: 3) */
LEXER_ASSIGN_LEFT_SHIFT, /**< "<<=" (prec: 3) */
LEXER_ASSIGN_RIGHT_SHIFT, /**< ">>=" (prec: 3) */
LEXER_ASSIGN_UNS_RIGHT_SHIFT, /**< ">>>=" (prec: 3) */
LEXER_ASSIGN_BIT_AND, /**< "&=" (prec: 3) */
LEXER_ASSIGN_BIT_OR, /**< "|=" (prec: 3) */
LEXER_ASSIGN_BIT_XOR, /**< "^=" (prec: 3) */
LEXER_QUESTION_MARK, /**< "?" (prec: 4) */
LEXER_LOGICAL_OR, /**< "||" (prec: 5) */
LEXER_LOGICAL_AND, /**< "&&" (prec: 6) */
LEXER_BIT_OR, /**< "|" (prec: 7) */
LEXER_BIT_XOR, /**< "^" (prec: 8) */
LEXER_BIT_AND, /**< "&" (prec: 9) */
LEXER_EQUAL, /**< "==" (prec: 10) */
LEXER_NOT_EQUAL, /**< "!=" (prec: 10) */
LEXER_STRICT_EQUAL, /**< "===" (prec: 10) */
LEXER_STRICT_NOT_EQUAL, /**< "!==" (prec: 10) */
LEXER_LESS, /**< "<" (prec: 11) */
LEXER_GREATER, /**< ">" (prec: 11) */
LEXER_LESS_EQUAL, /**< "<=" (prec: 11) */
LEXER_GREATER_EQUAL, /**< ">=" (prec: 11) */
LEXER_KEYW_IN, /**< in (prec: 11) */
LEXER_KEYW_INSTANCEOF, /**< instanceof (prec: 11) */
LEXER_LEFT_SHIFT, /**< "<<" (prec: 12) */
LEXER_RIGHT_SHIFT, /**< ">>" (prec: 12) */
LEXER_UNS_RIGHT_SHIFT, /**< ">>>" (prec: 12) */
LEXER_ADD, /**< "+" (prec: 13) */
LEXER_SUBTRACT, /**< "-" (prec: 13) */
LEXER_MULTIPLY, /**< "*" (prec: 14) */
LEXER_DIVIDE, /**< "/" (prec: 14) */
LEXER_MODULO, /**< "%" (prec: 14) */
LEXER_LEFT_BRACE, /**< "{" */
LEXER_LEFT_PAREN, /**< "(" */
LEXER_LEFT_SQUARE, /**< "[" */
LEXER_RIGHT_BRACE, /**< "}" */
LEXER_RIGHT_PAREN, /**<_")" */
LEXER_RIGHT_SQUARE, /**< "]" */
LEXER_DOT, /**< "." */
LEXER_SEMICOLON, /**< ";" */
LEXER_COLON, /**< ":" */
LEXER_COMMA, /**< "," */
LEXER_KEYW_BREAK, /**< break */
LEXER_KEYW_DO, /**< do */
LEXER_KEYW_CASE, /**< case */
LEXER_KEYW_ELSE, /**< else */
LEXER_KEYW_NEW, /**< new */
LEXER_KEYW_VAR, /**< var */
LEXER_KEYW_CATCH, /**< catch */
LEXER_KEYW_FINALLY, /**< finally */
LEXER_KEYW_RETURN, /**< return */
LEXER_KEYW_CONTINUE, /**< continue */
LEXER_KEYW_FOR, /**< for */
LEXER_KEYW_SWITCH, /**< switch */
LEXER_KEYW_WHILE, /**< while */
LEXER_KEYW_DEBUGGER, /**< debugger */
LEXER_KEYW_FUNCTION, /**< function */
LEXER_KEYW_WITH, /**< with */
LEXER_KEYW_DEFAULT, /**< default */
LEXER_KEYW_IF, /**< if */
LEXER_KEYW_THROW, /**< throw */
LEXER_KEYW_TRY, /**< try */
/* These are virtual tokens. */
LEXER_EXPRESSION_START, /**< expression start */
LEXER_PROPERTY_GETTER, /**< property getter function */
LEXER_PROPERTY_SETTER, /**< property setter function */
LEXER_COMMA_SEP_LIST, /**< comma separated bracketed expression list */
LEXER_SCAN_SWITCH, /**< special value for switch pre-scan */
/* Future reserved words: these keywords
* must form a group after all other keywords. */
#define LEXER_FIRST_FUTURE_RESERVED_WORD LEXER_KEYW_CLASS
LEXER_KEYW_CLASS, /**< class */
LEXER_KEYW_ENUM, /**< enum */
LEXER_KEYW_EXTENDS, /**< extends */
LEXER_KEYW_SUPER, /**< super */
LEXER_KEYW_CONST, /**< const */
LEXER_KEYW_EXPORT, /**< export */
LEXER_KEYW_IMPORT, /**< import */
/* Future strict reserved words: these keywords
* must form a group after future reserved words. */
#define LEXER_FIRST_FUTURE_STRICT_RESERVED_WORD LEXER_KEYW_IMPLEMENTS
LEXER_KEYW_IMPLEMENTS, /**< implements */
LEXER_KEYW_LET, /**< let */
LEXER_KEYW_PRIVATE, /**< private */
LEXER_KEYW_PUBLIC, /**< public */
LEXER_KEYW_YIELD, /**< yield */
LEXER_KEYW_INTERFACE, /**< interface */
LEXER_KEYW_PACKAGE, /**< package */
LEXER_KEYW_PROTECTED, /**< protected */
LEXER_KEYW_STATIC, /**< static */
} lexer_token_type_t;
#define LEXER_NEWLINE_LS_PS_BYTE_1 0xe2
#define LEXER_NEWLINE_LS_PS_BYTE_23(source) \
((source)[1] == LIT_UTF8_2_BYTE_CODE_POINT_MIN && ((source)[2] | 0x1) == 0xa9)
#define LEXER_UTF8_4BYTE_START 0xf0
#define LEXER_IS_LEFT_BRACKET(type) \
((type) == LEXER_LEFT_BRACE || (type) == LEXER_LEFT_PAREN || (type) == LEXER_LEFT_SQUARE)
#define LEXER_IS_RIGHT_BRACKET(type) \
((type) == LEXER_RIGHT_BRACE || (type) == LEXER_RIGHT_PAREN || (type) == LEXER_RIGHT_SQUARE)
#define LEXER_UNARY_OP_TOKEN_TO_OPCODE(token_type) \
((((token_type) - LEXER_PLUS) * 2) + CBC_PLUS)
#define LEXER_UNARY_LVALUE_OP_TOKEN_TO_OPCODE(token_type) \
((((token_type) - LEXER_INCREASE) * 6) + CBC_PRE_INCR)
#define LEXER_BINARY_OP_TOKEN_TO_OPCODE(token_type) \
((cbc_opcode_t) ((((token_type) - LEXER_BIT_OR) * 3) + CBC_BIT_OR))
#define LEXER_BINARY_LVALUE_OP_TOKEN_TO_OPCODE(token_type) \
((cbc_opcode_t) ((((token_type) - LEXER_ASSIGN_ADD) * 2) + CBC_ASSIGN_ADD))
/**
* Lexer literal object types.
*/
typedef enum
{
LEXER_LITERAL_OBJECT_ANY, /**< unspecified object type */
LEXER_LITERAL_OBJECT_EVAL, /**< reference is equal to eval */
LEXER_LITERAL_OBJECT_ARGUMENTS, /**< reference is equal to arguments */
} lexer_literal_object_type_t;
/**
* Lexer number types.
*/
typedef enum
{
LEXER_NUMBER_DECIMAL, /**< decimal number */
LEXER_NUMBER_HEXADECIMAL, /**< hexadecimal number */
LEXER_NUMBER_OCTAL, /**< octal number */
} lexer_number_type_t;
/**
* Lexer character (string / identifier) literal data.
*/
typedef struct
{
const uint8_t *char_p; /**< start of identifier or string token */
uint16_t length; /**< length or index of a literal */
uint8_t type; /**< type of the current literal */
uint8_t has_escape; /**< has escape sequences */
} lexer_lit_location_t;
/**
* Range of input string which processing is postponed.
*/
typedef struct
{
const uint8_t *source_p; /**< next source byte */
const uint8_t *source_end_p; /**< last source byte */
parser_line_counter_t line; /**< token start line */
parser_line_counter_t column; /**< token start column */
} lexer_range_t;
/**
* Lexer token.
*/
typedef struct
{
uint8_t type; /**< token type */
uint8_t literal_is_reserved; /**< future reserved keyword
* (when char_literal.type is LEXER_IDENT_LITERAL) */
uint8_t extra_value; /**< helper value for different purposes */
uint8_t was_newline; /**< newline occured before this token */
parser_line_counter_t line; /**< token start line */
parser_line_counter_t column; /**< token start column */
lexer_lit_location_t lit_location; /**< extra data for character literals */
} lexer_token_t;
/**
* Literal data set by lexer_construct_literal_object.
*/
typedef struct
{
lexer_literal_t *literal_p; /**< pointer to the literal object */
uint16_t index; /**< literal index */
uint8_t type; /**< literal object type */
} lexer_lit_object_t;
/**
* @}
* @}
* @}
*/
#endif /* !JS_LEXER_H */

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,436 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef JS_PARSER_INTERNAL_H
#define JS_PARSER_INTERNAL_H
#include "common.h"
#include "byte-code.h"
#include "js-parser.h"
#include "js-parser-limits.h"
#include "js-lexer.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_internals Internals
* @{
*/
/* General parser flags. */
#define PARSER_IS_STRICT 0x00001u
#define PARSER_IS_FUNCTION 0x00002u
#define PARSER_IS_CLOSURE 0x00004u
#define PARSER_IS_PROPERTY_GETTER 0x00008u
#define PARSER_IS_PROPERTY_SETTER 0x00010u
#define PARSER_IS_FUNC_EXPRESSION 0x00020u
#define PARSER_HAS_NON_STRICT_ARG 0x00040u
#define PARSER_INSIDE_WITH 0x00080u
#define PARSER_RESOLVE_THIS_FOR_CALLS 0x00100u
#define PARSER_NAMED_FUNCTION_EXP 0x00200u
#define PARSER_HAS_INITIALIZED_VARS 0x00400u
#define PARSER_NO_END_LABEL 0x00800u
#define PARSER_NO_REG_STORE 0x01000u
#define PARSER_ARGUMENTS_NEEDED 0x02000u
#define PARSER_ARGUMENTS_NOT_NEEDED 0x04000u
#define PARSER_LEXICAL_ENV_NEEDED 0x08000u
#define PARSER_HAS_LATE_LIT_INIT 0x10000u
/* Expression parsing flags. */
#define PARSE_EXPR 0x00
#define PARSE_EXPR_STATEMENT 0x01
#define PARSE_EXPR_BLOCK 0x02
#define PARSE_EXPR_NO_COMMA 0x04
#define PARSE_EXPR_HAS_LITERAL 0x08
/* The maximum of PARSER_CBC_STREAM_PAGE_SIZE is 127. */
#define PARSER_CBC_STREAM_PAGE_SIZE \
((uint32_t) (64 - sizeof (void *)))
#define PARSER_STACK_PAGE_SIZE \
((uint32_t) (((sizeof (void *) > 4) ? 128 : 64) - sizeof (void *)))
/* Avoid compiler warnings for += operations. */
#define PARSER_PLUS_EQUAL_U16(base, value) (base) = (uint16_t) ((base) + (value))
#define PARSER_MINUS_EQUAL_U16(base, value) (base) = (uint16_t) ((base) - (value))
#define PARSER_PLUS_EQUAL_LC(base, value) (base) = (parser_line_counter_t) ((base) + (value))
/**
* Argument for a compact-byte code.
*/
typedef struct
{
uint16_t literal_index; /**< literal index argument */
uint16_t value; /**< other argument (second literal or byte). */
uint16_t third_literal_index; /**< literal index argument */
uint8_t literal_type; /**< last literal type */
uint8_t literal_object_type; /**< last literal object type */
} cbc_argument_t;
/* Useful parser macros. */
#define PARSER_CBC_UNAVAILABLE CBC_EXT_OPCODE
#define PARSER_TO_EXT_OPCODE(opcode) ((uint16_t) ((opcode) + 256))
#define PARSER_GET_EXT_OPCODE(opcode) ((opcode) - 256)
#define PARSER_IS_BASIC_OPCODE(opcode) ((opcode) < 256)
#define PARSER_IS_PUSH_LITERAL(opcode) \
((opcode) == CBC_PUSH_LITERAL \
|| (opcode) == CBC_PUSH_TWO_LITERALS \
|| (opcode) == CBC_PUSH_THREE_LITERALS)
#define PARSER_GET_LITERAL(literal_index) \
((lexer_literal_t *) parser_list_get (&context_p->literal_pool, (literal_index)))
#define PARSER_TO_BINARY_OPERATION_WITH_RESULT(opcode) \
(PARSER_TO_EXT_OPCODE(opcode) - CBC_ASSIGN_ADD + CBC_EXT_ASSIGN_ADD_PUSH_RESULT)
#define PARSER_TO_BINARY_OPERATION_WITH_BLOCK(opcode) \
((uint16_t) (PARSER_TO_EXT_OPCODE(opcode) - CBC_ASSIGN_ADD + CBC_EXT_ASSIGN_ADD_BLOCK))
#define PARSER_GET_FLAGS(op) \
(PARSER_IS_BASIC_OPCODE (op) ? cbc_flags[(op)] : cbc_ext_flags[PARSER_GET_EXT_OPCODE (op)])
#define PARSER_OPCODE_IS_RETURN(op) \
((op) == CBC_RETURN || (op) == CBC_RETURN_WITH_BLOCK || (op) == CBC_RETURN_WITH_LITERAL)
#define PARSER_ARGS_EQ(op, types) \
((PARSER_GET_FLAGS (op) & CBC_ARG_TYPES) == (types))
/**
* All data allocated by the parser is
* stored in parser_data_pages in the memory.
*/
typedef struct parser_mem_page_t
{
struct parser_mem_page_t *next_p; /**< next page */
uint8_t bytes[1]; /**< memory bytes */
} parser_mem_page_t;
/**
* Structure for managing parser memory.
*/
typedef struct
{
parser_mem_page_t *first_p; /**< first allocated page */
parser_mem_page_t *last_p; /**< last allocated page */
uint32_t last_position; /**< position of the last allocated byte */
} parser_mem_data_t;
/**
* Parser memory list.
*/
typedef struct
{
parser_mem_data_t data; /**< storage space */
uint32_t page_size; /**< size of each page */
uint32_t item_size; /**< size of each item */
uint32_t item_count; /**< number of items on each page */
} parser_list_t;
/**
* Iterator for parser memory list.
*/
typedef struct
{
parser_list_t *list_p; /**< parser list */
parser_mem_page_t *current_p; /**< currently processed page */
size_t current_position; /**< current position on the page */
} parser_list_iterator_t;
/**
* Parser memory stack.
*/
typedef struct
{
parser_mem_data_t data; /**< storage space */
parser_mem_page_t *free_page_p; /**< space for fast allocation */
} parser_stack_t;
/**
* Iterator for parser memory stack.
*/
typedef struct
{
parser_mem_page_t *current_p; /**< currently processed page */
size_t current_position; /**< current position on the page */
} parser_stack_iterator_t;
/**
* Branch type.
*/
typedef struct
{
parser_mem_page_t *page_p; /**< branch location page */
uint32_t offset; /**< branch location offset */
} parser_branch_t;
/**
* Branch chain type.
*/
typedef struct parser_branch_node_t
{
struct parser_branch_node_t *next_p; /**< next linked list node */
parser_branch_t branch; /**< branch */
} parser_branch_node_t;
/**
* Those members of a context which needs
* to be saved when a sub-function is parsed.
*/
typedef struct parser_saved_context_t
{
/* Parser members. */
uint32_t status_flags; /**< parsing options */
uint16_t stack_depth; /**< current stack depth */
uint16_t stack_limit; /**< maximum stack depth */
struct parser_saved_context_t *prev_context_p; /**< last saved context */
parser_stack_iterator_t last_statement; /**< last statement position */
/* Literal types */
uint16_t argument_count; /**< number of function arguments */
uint16_t register_count; /**< number of registers */
uint16_t literal_count; /**< number of literals */
/* Memory storage members. */
parser_mem_data_t byte_code; /**< byte code buffer */
uint32_t byte_code_size; /**< byte code size for branches */
parser_mem_data_t literal_pool_data; /**< literal list */
#ifndef JERRY_NDEBUG
uint16_t context_stack_depth; /**< current context stack depth */
#endif /* !JERRY_NDEBUG */
} parser_saved_context_t;
/**
* Shared parser context.
*/
typedef struct
{
PARSER_TRY_CONTEXT (try_buffer); /**< try_buffer */
parser_error_t error; /**< error code */
void *allocated_buffer_p; /**< dinamically allocated buffer
* which needs to be freed on error */
uint32_t allocated_buffer_size; /**< size of the dinamically allocated buffer */
/* Parser members. */
uint32_t status_flags; /**< status flags */
uint16_t stack_depth; /**< current stack depth */
uint16_t stack_limit; /**< maximum stack depth */
parser_saved_context_t *last_context_p; /**< last saved context */
parser_stack_iterator_t last_statement; /**< last statement position */
/* Lexer members. */
lexer_token_t token; /**< current token */
lexer_lit_object_t lit_object; /**< current literal object */
const uint8_t *source_p; /**< next source byte */
const uint8_t *source_end_p; /**< last source byte */
parser_line_counter_t line; /**< current line */
parser_line_counter_t column; /**< current column */
/* Compact byte code members. */
cbc_argument_t last_cbc; /**< argument of the last cbc */
uint16_t last_cbc_opcode; /**< opcode of the last cbc */
/* Literal types */
uint16_t argument_count; /**< number of function arguments */
uint16_t register_count; /**< number of registers */
uint16_t literal_count; /**< number of literals */
/* Memory storage members. */
parser_mem_data_t byte_code; /**< byte code buffer */
uint32_t byte_code_size; /**< current byte code size for branches */
parser_list_t literal_pool; /**< literal list */
parser_mem_data_t stack; /**< storage space */
parser_mem_page_t *free_page_p; /**< space for fast allocation */
uint8_t stack_top_uint8; /**< top byte stored on the stack */
#ifndef JERRY_NDEBUG
/* Variables for debugging / logging. */
uint16_t context_stack_depth; /**< current context stack depth */
#endif /* !JERRY_NDEBUG */
#ifdef PARSER_DUMP_BYTE_CODE
int is_show_opcodes; /**< show opcodes */
uint32_t total_byte_code_size; /**< total byte code size */
#endif /* PARSER_DUMP_BYTE_CODE */
} parser_context_t;
/**
* @}
* @}
* @}
*
* \addtogroup mem Memory allocation
* @{
*
* \addtogroup mem_parser Parser memory manager
* @{
*/
/* Memory management.
* Note: throws an error if unsuccessful. */
void *parser_malloc (parser_context_t *, size_t);
void parser_free (void *, size_t);
void *parser_malloc_local (parser_context_t *, size_t);
void parser_free_local (void *, size_t);
/* Parser byte stream. */
void parser_cbc_stream_init (parser_mem_data_t *);
void parser_cbc_stream_free (parser_mem_data_t *);
void parser_cbc_stream_alloc_page (parser_context_t *, parser_mem_data_t *);
/* Parser list. Ensures pointer alignment. */
void parser_list_init (parser_list_t *, uint32_t, uint32_t);
void parser_list_free (parser_list_t *);
void parser_list_reset (parser_list_t *);
void *parser_list_append (parser_context_t *, parser_list_t *);
void *parser_list_get (parser_list_t *, size_t);
void parser_list_iterator_init (parser_list_t *, parser_list_iterator_t *);
void *parser_list_iterator_next (parser_list_iterator_t *);
/* Parser stack. Optimized for pushing bytes.
* Pop functions never throws error. */
void parser_stack_init (parser_context_t *);
void parser_stack_free (parser_context_t *);
void parser_stack_push_uint8 (parser_context_t *, uint8_t);
void parser_stack_pop_uint8 (parser_context_t *);
void parser_stack_push_uint16 (parser_context_t *, uint16_t);
uint16_t parser_stack_pop_uint16 (parser_context_t *);
void parser_stack_push (parser_context_t *, const void *, uint32_t);
void parser_stack_pop (parser_context_t *, void *, uint32_t);
void parser_stack_iterator_skip (parser_stack_iterator_t *, size_t);
void parser_stack_iterator_read (parser_stack_iterator_t *, void *, size_t);
void parser_stack_iterator_write (parser_stack_iterator_t *, const void *, size_t);
/**
* @}
* @}
*
* \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_utils Utility
* @{
*/
/* Compact byte code emitting functions. */
void parser_flush_cbc (parser_context_t *);
void parser_emit_cbc (parser_context_t *, uint16_t);
void parser_emit_cbc_literal (parser_context_t *, uint16_t, uint16_t);
void parser_emit_cbc_literal_from_token (parser_context_t *, uint16_t);
void parser_emit_cbc_call (parser_context_t *, uint16_t, size_t);
void parser_emit_cbc_push_number (parser_context_t *, bool);
void parser_emit_cbc_forward_branch (parser_context_t *, uint16_t, parser_branch_t *);
parser_branch_node_t *parser_emit_cbc_forward_branch_item (parser_context_t *, uint16_t, parser_branch_node_t *);
void parser_emit_cbc_backward_branch (parser_context_t *, uint16_t, uint32_t);
void parser_set_branch_to_current_position (parser_context_t *, parser_branch_t *);
void parser_set_breaks_to_current_position (parser_context_t *, parser_branch_node_t *);
void parser_set_continues_to_current_position (parser_context_t *, parser_branch_node_t *);
/* Convenience macros. */
#define parser_emit_cbc_ext(context_p, opcode) \
parser_emit_cbc ((context_p), PARSER_TO_EXT_OPCODE (opcode))
#define parser_emit_cbc_ext_literal(context_p, opcode, literal_index) \
parser_emit_cbc_literal ((context_p), PARSER_TO_EXT_OPCODE (opcode), (literal_index))
#define parser_emit_cbc_ext_call(context_p, opcode, call_arguments) \
parser_emit_cbc_call ((context_p), PARSER_TO_EXT_OPCODE (opcode), (call_arguments))
#define parser_emit_cbc_ext_forward_branch(context_p, opcode, branch_p) \
parser_emit_cbc_forward_branch ((context_p), PARSER_TO_EXT_OPCODE (opcode), (branch_p))
#define parser_emit_cbc_ext_backward_branch(context_p, opcode, offset) \
parser_emit_cbc_backward_branch ((context_p), PARSER_TO_EXT_OPCODE (opcode), (offset))
/**
* @}
*
* \addtogroup jsparser_lexer Lexer
* @{
*/
/* Lexer functions */
void lexer_next_token (parser_context_t *);
void lexer_expect_identifier (parser_context_t *, uint8_t);
void lexer_scan_identifier (parser_context_t *, bool);
ecma_char_t lexer_hex_to_character (parser_context_t *context_p, const uint8_t *source_p, int length);
void lexer_expect_object_literal_id (parser_context_t *, bool);
void lexer_construct_literal_object (parser_context_t *, lexer_lit_location_t *, uint8_t);
bool lexer_construct_number_object (parser_context_t *, bool, bool);
void lexer_construct_function_object (parser_context_t *, uint32_t);
void lexer_construct_regexp_object (parser_context_t *, bool);
bool lexer_compare_identifier_to_current (parser_context_t *, const lexer_lit_location_t *);
/**
* @}
*
* \addtogroup jsparser_expr Expression parser
* @{
*/
/* Parser functions. */
void parser_parse_expression (parser_context_t *, int);
/**
* @}
*
* \addtogroup jsparser_scanner Scanner
* @{
*/
void parser_scan_until (parser_context_t *, lexer_range_t *, lexer_token_type_t);
/**
* @}
*
* \addtogroup jsparser_stmt Statement parser
* @{
*/
void parser_parse_statements (parser_context_t *);
void parser_free_jumps (parser_stack_iterator_t);
/**
* @}
*
* \addtogroup jsparser_parser Parser
* @{
*/
ecma_compiled_code_t *parser_parse_function (parser_context_t *, uint32_t);
/* Error management. */
void parser_raise_error (parser_context_t *, parser_error_t);
/**
* @}
* @}
* @}
*/
#endif /* !JS_PARSER_INTERNAL_H */

View file

@ -0,0 +1,99 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef JS_PARSER_LIMITS_H
#define JS_PARSER_LIMITS_H
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_internals Internals
* @{
*/
/* Maximum identifier length accepted by the parser.
* Limit: LEXER_MAX_STRING_LENGTH. */
#ifndef PARSER_MAXIMUM_IDENT_LENGTH
#define PARSER_MAXIMUM_IDENT_LENGTH 255
#endif /* !PARSER_MAXIMUM_IDENT_LENGTH */
/* Maximum string length.
* Limit: 65535. */
#ifndef PARSER_MAXIMUM_STRING_LENGTH
#define PARSER_MAXIMUM_STRING_LENGTH 65535
#endif /* !PARSER_MAXIMUM_STRING_LENGTH */
/* Maximum number of literals.
* Limit: 32767. Recommended: 510, 32767 */
#ifndef PARSER_MAXIMUM_NUMBER_OF_LITERALS
#define PARSER_MAXIMUM_NUMBER_OF_LITERALS 32767
#endif /* !PARSER_MAXIMUM_NUMBER_OF_LITERALS */
/* Maximum number of registers.
* Limit: PARSER_MAXIMUM_NUMBER_OF_LITERALS */
#ifndef PARSER_MAXIMUM_NUMBER_OF_REGISTERS
#define PARSER_MAXIMUM_NUMBER_OF_REGISTERS 256
#endif /* !PARSER_MAXIMUM_NUMBER_OF_REGISTERS */
/* Maximum code size.
* Limit: 16777215. Recommended: 65535, 16777215. */
#ifndef PARSER_MAXIMUM_CODE_SIZE
#define PARSER_MAXIMUM_CODE_SIZE (65535 << (JMEM_ALIGNMENT_LOG))
#endif /* !PARSER_MAXIMUM_CODE_SIZE */
/* Maximum number of values pushed onto the stack by a function.
* Limit: 65500. Recommended: 1024. */
#ifndef PARSER_MAXIMUM_STACK_LIMIT
#define PARSER_MAXIMUM_STACK_LIMIT 1024
#endif /* !PARSER_MAXIMUM_STACK_LIMIT */
/* Checks. */
#if (PARSER_MAXIMUM_STRING_LENGTH < 1) || (PARSER_MAXIMUM_STRING_LENGTH > 65535)
#error "Maximum string length is not within range."
#endif /* (PARSER_MAXIMUM_STRING_LENGTH < 1) || (PARSER_MAXIMUM_STRING_LENGTH > 65535) */
#if (PARSER_MAXIMUM_IDENT_LENGTH < 1) || (PARSER_MAXIMUM_IDENT_LENGTH > PARSER_MAXIMUM_STRING_LENGTH)
#error "Maximum identifier length is not within range."
#endif /* (PARSER_MAXIMUM_IDENT_LENGTH < 1) || (PARSER_MAXIMUM_IDENT_LENGTH > PARSER_MAXIMUM_STRING_LENGTH) */
#if (PARSER_MAXIMUM_NUMBER_OF_LITERALS < 1) || (PARSER_MAXIMUM_NUMBER_OF_LITERALS > 32767)
#error "Maximum number of literals is not within range."
#endif /* (PARSER_MAXIMUM_NUMBER_OF_LITERALS < 1) || (PARSER_MAXIMUM_NUMBER_OF_LITERALS > 32767) */
#if (PARSER_MAXIMUM_NUMBER_OF_REGISTERS > PARSER_MAXIMUM_NUMBER_OF_LITERALS)
#error "Maximum number of registers is not within range."
#endif /* (PARSER_MAXIMUM_NUMBER_OF_REGISTERS > PARSER_MAXIMUM_NUMBER_OF_LITERALS) */
#if (PARSER_MAXIMUM_CODE_SIZE < 4096) || (PARSER_MAXIMUM_CODE_SIZE > 16777215)
#error "Maximum code size is not within range."
#endif /* (PARSER_MAXIMUM_CODE_SIZE < 4096) || (PARSER_MAXIMUM_CODE_SIZE > 16777215) */
#if (PARSER_MAXIMUM_STACK_LIMIT < 16) || (PARSER_MAXIMUM_STACK_LIMIT > 65500)
#error "Maximum function stack usage is not within range."
#endif /* (PARSER_MAXIMUM_STACK_LIMIT < 16) || (PARSER_MAXIMUM_STACK_LIMIT > 65500) */
/**
* @}
* @}
* @}
*/
#endif /* !JS_PARSER_LIMITS_H */

View file

@ -0,0 +1,678 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js-parser-internal.h"
/** \addtogroup mem Memory allocation
* @{
*
* \addtogroup mem_parser Parser memory manager
* @{
*/
/**********************************************************************/
/* Memory allocation */
/**********************************************************************/
/**
* Allocate memory.
*
* @return allocated memory.
*/
void *
parser_malloc (parser_context_t *context_p, /**< context */
size_t size) /**< size of the memory block */
{
void *result;
JERRY_ASSERT (size > 0);
result = jmem_heap_alloc_block_null_on_error (size);
if (result == NULL)
{
parser_raise_error (context_p, PARSER_ERR_OUT_OF_MEMORY);
}
return result;
} /* parser_malloc */
/**
* Free memory allocated by parser_malloc.
*/
void parser_free (void *ptr, /**< pointer to free */
size_t size) /**< size of the memory block */
{
jmem_heap_free_block (ptr, size);
} /* parser_free */
/**
* Allocate local memory for short term use.
*
* @return allocated memory.
*/
void *
parser_malloc_local (parser_context_t *context_p, /**< context */
size_t size) /**< size of the memory */
{
void *result;
JERRY_ASSERT (size > 0);
result = jmem_heap_alloc_block (size);
if (result == 0)
{
parser_raise_error (context_p, PARSER_ERR_OUT_OF_MEMORY);
}
return result;
} /* parser_malloc_local */
/**
* Free memory allocated by parser_malloc_local.
*/
void parser_free_local (void *ptr, /**< pointer to free */
size_t size) /**< size of the memory */
{
jmem_heap_free_block (ptr, size);
} /* parser_free_local */
/**********************************************************************/
/* Parser data management functions */
/**********************************************************************/
/**
* Initialize parse data.
*/
static void
parser_data_init (parser_mem_data_t *data_p, /**< memory manager */
uint32_t page_size) /**< size of each page */
{
data_p->first_p = NULL;
data_p->last_p = NULL;
data_p->last_position = page_size;
} /* parser_data_init */
/**
* Free parse data.
*/
static void
parser_data_free (parser_mem_data_t *data_p, /**< memory manager */
uint32_t page_size) /**< size of each page */
{
parser_mem_page_t *page_p = data_p->first_p;
while (page_p != NULL)
{
parser_mem_page_t *next_p = page_p->next_p;
parser_free (page_p, page_size);
page_p = next_p;
}
} /* parser_data_free */
/**********************************************************************/
/* Parser byte stream management functions */
/**********************************************************************/
/**
* Initialize byte stream.
*/
void
parser_cbc_stream_init (parser_mem_data_t *data_p) /**< memory manager */
{
parser_data_init (data_p, PARSER_CBC_STREAM_PAGE_SIZE);
} /* parser_cbc_stream_init */
/**
* Free byte stream.
*/
void
parser_cbc_stream_free (parser_mem_data_t *data_p) /**< memory manager */
{
parser_data_free (data_p,
sizeof (parser_mem_page_t *) + PARSER_CBC_STREAM_PAGE_SIZE);
} /* parser_cbc_stream_free */
/**
* Appends a byte at the end of the byte stream.
*/
void
parser_cbc_stream_alloc_page (parser_context_t *context_p, /**< context */
parser_mem_data_t *data_p) /**< memory manager */
{
size_t size = sizeof (parser_mem_page_t *) + PARSER_CBC_STREAM_PAGE_SIZE;
parser_mem_page_t *page_p = (parser_mem_page_t *) parser_malloc (context_p, size);
page_p->next_p = NULL;
data_p->last_position = 0;
if (data_p->last_p != NULL)
{
data_p->last_p->next_p = page_p;
}
else
{
data_p->first_p = page_p;
}
data_p->last_p = page_p;
} /* parser_cbc_stream_alloc_page */
/**********************************************************************/
/* Parser list management functions */
/**********************************************************************/
/**
* Initialize parser list.
*/
void
parser_list_init (parser_list_t *list_p, /**< parser list */
uint32_t item_size, /**< size for each page */
uint32_t item_count) /**< number of items on each page */
{
/* Align to pointer size. */
item_size = (uint32_t) (((item_size) + sizeof (void *) - 1) & ~(sizeof (void *) - 1));
parser_data_init (&list_p->data, item_size * item_count);
list_p->page_size = item_size * item_count;
list_p->item_size = item_size;
list_p->item_count = item_count;
} /* parser_list_init */
/**
* Free parser list.
*/
void
parser_list_free (parser_list_t *list_p) /**< parser list */
{
parser_data_free (&list_p->data,
(uint32_t) (sizeof (parser_mem_page_t *) + list_p->page_size));
} /* parser_list_free */
/**
* Reset parser list.
*/
void
parser_list_reset (parser_list_t *list_p) /**< parser list */
{
parser_data_init (&list_p->data, list_p->page_size);
} /* parser_list_reset */
/**
* Allocate space for the next item.
*
* @return pointer to the appended item.
*/
void *
parser_list_append (parser_context_t *context_p, /**< context */
parser_list_t *list_p) /**< parser list */
{
parser_mem_page_t *page_p = list_p->data.last_p;
void *result;
if (list_p->data.last_position + list_p->item_size > list_p->page_size)
{
size_t size = sizeof (parser_mem_page_t *) + list_p->page_size;
page_p = (parser_mem_page_t *) parser_malloc (context_p, size);
page_p->next_p = NULL;
list_p->data.last_position = 0;
if (list_p->data.last_p != NULL)
{
list_p->data.last_p->next_p = page_p;
}
else
{
list_p->data.first_p = page_p;
}
list_p->data.last_p = page_p;
}
result = page_p->bytes + list_p->data.last_position;
list_p->data.last_position += list_p->item_size;
return result;
} /* parser_list_append */
/**
* Return the nth item of the list.
*
* @return pointer to the item.
*/
void *
parser_list_get (parser_list_t *list_p, /**< parser list */
size_t index) /**< item index */
{
size_t item_count = list_p->item_count;
parser_mem_page_t *page_p = list_p->data.first_p;
while (index >= item_count)
{
JERRY_ASSERT (page_p != NULL);
page_p = page_p->next_p;
index -= item_count;
}
JERRY_ASSERT (page_p != NULL);
JERRY_ASSERT (page_p != list_p->data.last_p
|| (index * list_p->item_size < list_p->data.last_position));
return page_p->bytes + (index * list_p->item_size);
} /* parser_list_get */
/**
* Initialize a parser list iterator.
*/
void
parser_list_iterator_init (parser_list_t *list_p, /**< parser list */
parser_list_iterator_t *iterator_p) /**< iterator */
{
iterator_p->list_p = list_p;
iterator_p->current_p = list_p->data.first_p;
iterator_p->current_position = 0;
} /* parser_list_iterator_init */
/**
* Next iterator step.
*
* @return the address of the current item, or NULL at the end.
*/
void *
parser_list_iterator_next (parser_list_iterator_t *iterator_p) /**< iterator */
{
void *result;
if (iterator_p->current_p == NULL)
{
return NULL;
}
result = iterator_p->current_p->bytes + iterator_p->current_position;
iterator_p->current_position += iterator_p->list_p->item_size;
if (iterator_p->current_p->next_p == NULL)
{
if (iterator_p->current_position >= iterator_p->list_p->data.last_position)
{
iterator_p->current_p = NULL;
iterator_p->current_position = 0;
}
}
else if (iterator_p->current_position >= iterator_p->list_p->page_size)
{
iterator_p->current_p = iterator_p->current_p->next_p;
iterator_p->current_position = 0;
}
return result;
} /* parser_list_iterator_next */
/**********************************************************************/
/* Parser stack management functions */
/**********************************************************************/
/* Stack is a reversed storage. */
/**
* Initialize parser stack.
*/
void
parser_stack_init (parser_context_t *context_p) /**< context */
{
parser_data_init (&context_p->stack, PARSER_STACK_PAGE_SIZE);
context_p->free_page_p = NULL;
} /* parser_stack_init */
/**
* Free parser stack.
*/
void
parser_stack_free (parser_context_t *context_p) /**< context */
{
parser_data_free (&context_p->stack,
sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE);
if (context_p->free_page_p != NULL)
{
parser_free (context_p->free_page_p,
sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE);
}
} /* parser_stack_free */
/**
* Pushes an uint8_t value onto the stack.
*/
void
parser_stack_push_uint8 (parser_context_t *context_p, /**< context */
uint8_t uint8_value) /**< value pushed onto the stack */
{
parser_mem_page_t *page_p = context_p->stack.first_p;
/* This assert might trigger false positive valgrind errors, when
* parser_stack_push() pushes not fully initialized structures.
* More precisely when the last byte of the structure is uninitialized. */
JERRY_ASSERT (page_p == NULL
|| context_p->stack_top_uint8 == page_p->bytes[context_p->stack.last_position - 1]);
if (context_p->stack.last_position >= PARSER_STACK_PAGE_SIZE)
{
if (context_p->free_page_p != NULL)
{
page_p = context_p->free_page_p;
context_p->free_page_p = NULL;
}
else
{
size_t size = sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE;
page_p = (parser_mem_page_t *) parser_malloc (context_p, size);
}
page_p->next_p = context_p->stack.first_p;
context_p->stack.last_position = 0;
context_p->stack.first_p = page_p;
}
page_p->bytes[context_p->stack.last_position++] = uint8_value;
context_p->stack_top_uint8 = uint8_value;
} /* parser_stack_push_uint8 */
/**
* Pops the last uint8_t value from the stack.
*/
void
parser_stack_pop_uint8 (parser_context_t *context_p) /**< context */
{
parser_mem_page_t *page_p = context_p->stack.first_p;
JERRY_ASSERT (page_p != NULL
&& context_p->stack_top_uint8 == page_p->bytes[context_p->stack.last_position - 1]);
context_p->stack.last_position--;
if (context_p->stack.last_position == 0)
{
context_p->stack.first_p = page_p->next_p;
context_p->stack.last_position = PARSER_STACK_PAGE_SIZE;
if (context_p->free_page_p == NULL)
{
context_p->free_page_p = page_p;
}
else
{
parser_free (page_p,
sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE);
}
page_p = context_p->stack.first_p;
JERRY_ASSERT (page_p != NULL);
}
context_p->stack_top_uint8 = page_p->bytes[context_p->stack.last_position - 1];
} /* parser_stack_pop_uint8 */
/**
* Pushes an uint16_t value onto the stack.
*/
void
parser_stack_push_uint16 (parser_context_t *context_p, /**< context */
uint16_t uint16_value) /**< value pushed onto the stack */
{
if (context_p->stack.last_position + 2 <= PARSER_STACK_PAGE_SIZE)
{
parser_mem_page_t *page_p = context_p->stack.first_p;
JERRY_ASSERT (page_p != NULL
&& context_p->stack_top_uint8 == page_p->bytes[context_p->stack.last_position - 1]);
page_p->bytes[context_p->stack.last_position++] = (uint8_t) (uint16_value >> 8);
page_p->bytes[context_p->stack.last_position++] = (uint8_t) uint16_value;
context_p->stack_top_uint8 = (uint8_t) uint16_value;
}
else
{
parser_stack_push_uint8 (context_p, (uint8_t) (uint16_value >> 8));
parser_stack_push_uint8 (context_p, (uint8_t) uint16_value);
}
} /* parser_stack_push_uint16 */
/**
* Pops the last uint16_t value from the stack.
*
* @return the value popped from the stack.
*/
uint16_t
parser_stack_pop_uint16 (parser_context_t *context_p) /**< context */
{
uint32_t value = context_p->stack_top_uint8;
if (context_p->stack.last_position >= 3)
{
parser_mem_page_t *page_p = context_p->stack.first_p;
JERRY_ASSERT (page_p != NULL
&& context_p->stack_top_uint8 == page_p->bytes[context_p->stack.last_position - 1]);
value |= ((uint32_t) page_p->bytes[context_p->stack.last_position - 2]) << 8;
context_p->stack_top_uint8 = page_p->bytes[context_p->stack.last_position - 3];
context_p->stack.last_position -= 2;
}
else
{
parser_stack_pop_uint8 (context_p);
value |= ((uint32_t) context_p->stack_top_uint8) << 8;
parser_stack_pop_uint8 (context_p);
}
return (uint16_t) value;
} /* parser_stack_pop_uint16 */
/**
* Pushes a data onto the stack.
*/
void
parser_stack_push (parser_context_t *context_p, /**< context */
const void *data_p, /**< data pushed onto the stack */
uint32_t length) /**< length of the data */
{
uint32_t fragment_length = PARSER_STACK_PAGE_SIZE - context_p->stack.last_position;
const uint8_t *bytes_p = (const uint8_t *) data_p;
parser_mem_page_t *page_p;
JERRY_ASSERT (length < PARSER_STACK_PAGE_SIZE && length > 0);
context_p->stack_top_uint8 = bytes_p[length - 1];
if (fragment_length > 0)
{
/* Fill the remaining bytes. */
if (fragment_length > length)
{
fragment_length = length;
}
memcpy (context_p->stack.first_p->bytes + context_p->stack.last_position,
bytes_p,
fragment_length);
if (fragment_length == length)
{
context_p->stack.last_position += length;
return;
}
bytes_p += fragment_length;
length -= fragment_length;
}
if (context_p->free_page_p != NULL)
{
page_p = context_p->free_page_p;
context_p->free_page_p = NULL;
}
else
{
size_t size = sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE;
page_p = (parser_mem_page_t *) parser_malloc (context_p, size);
}
page_p->next_p = context_p->stack.first_p;
context_p->stack.first_p = page_p;
memcpy (page_p->bytes, bytes_p, length);
context_p->stack.last_position = length;
} /* parser_stack_push */
/**
* Pop bytes from the top of the stack.
*/
void
parser_stack_pop (parser_context_t *context_p, /**< context */
void *data_p, /**< destination buffer, can be NULL */
uint32_t length) /**< length of the data */
{
uint8_t *bytes_p = (uint8_t *) data_p;
parser_mem_page_t *page_p = context_p->stack.first_p;
JERRY_ASSERT (length < PARSER_STACK_PAGE_SIZE && length > 0);
if (context_p->stack.last_position > length)
{
context_p->stack.last_position -= length;
context_p->stack_top_uint8 = page_p->bytes[context_p->stack.last_position - 1];
if (bytes_p != NULL)
{
memcpy (bytes_p, context_p->stack.first_p->bytes + context_p->stack.last_position, length);
}
return;
}
JERRY_ASSERT (page_p->next_p != NULL);
length -= context_p->stack.last_position;
if (bytes_p != NULL)
{
memcpy (bytes_p + length, page_p->bytes, context_p->stack.last_position);
}
context_p->stack.first_p = page_p->next_p;
context_p->stack.last_position = PARSER_STACK_PAGE_SIZE - length;
context_p->stack_top_uint8 = page_p->next_p->bytes[context_p->stack.last_position - 1];
if (bytes_p != NULL && length > 0)
{
memcpy (bytes_p, page_p->next_p->bytes + context_p->stack.last_position, length);
}
JERRY_ASSERT (context_p->stack.last_position > 0);
if (context_p->free_page_p == NULL)
{
context_p->free_page_p = page_p;
}
else
{
parser_free (page_p,
sizeof (parser_mem_page_t *) + PARSER_STACK_PAGE_SIZE);
}
} /* parser_stack_pop */
/**
* Skip the next n bytes of the stack.
*/
void
parser_stack_iterator_skip (parser_stack_iterator_t *iterator, /**< iterator */
size_t length) /**< number of skipped bytes */
{
JERRY_ASSERT (length < PARSER_STACK_PAGE_SIZE && length > 0);
if (length < iterator->current_position)
{
iterator->current_position -= length;
}
else
{
iterator->current_position = PARSER_STACK_PAGE_SIZE - (length - iterator->current_position);
iterator->current_p = iterator->current_p->next_p;
}
} /* parser_stack_iterator_skip */
/**
* Read bytes from the stack.
*/
void
parser_stack_iterator_read (parser_stack_iterator_t *iterator, /**< iterator */
void *data_p, /**< destination buffer */
size_t length) /**< length of the data */
{
uint8_t *bytes_p = (uint8_t *) data_p;
JERRY_ASSERT (length < PARSER_STACK_PAGE_SIZE && length > 0);
if (length <= iterator->current_position)
{
memcpy (bytes_p,
iterator->current_p->bytes + iterator->current_position - length,
length);
}
else
{
JERRY_ASSERT (iterator->current_p->next_p != NULL);
length -= iterator->current_position;
memcpy (bytes_p + length,
iterator->current_p->bytes,
iterator->current_position);
memcpy (bytes_p,
iterator->current_p->next_p->bytes + PARSER_STACK_PAGE_SIZE - length,
length);
}
} /* parser_stack_iterator_read */
/**
* Write bytes onto the stack.
*/
void
parser_stack_iterator_write (parser_stack_iterator_t *iterator, /**< iterator */
const void *data_p, /**< destination buffer */
size_t length) /**< length of the data */
{
const uint8_t *bytes_p = (const uint8_t *) data_p;
JERRY_ASSERT (length < PARSER_STACK_PAGE_SIZE && length > 0);
if (length <= iterator->current_position)
{
memcpy (iterator->current_p->bytes + iterator->current_position - length,
bytes_p,
length);
}
else
{
JERRY_ASSERT (iterator->current_p->next_p != NULL);
length -= iterator->current_position;
memcpy (iterator->current_p->bytes,
bytes_p + length,
iterator->current_position);
memcpy (iterator->current_p->next_p->bytes + PARSER_STACK_PAGE_SIZE - length,
bytes_p,
length);
}
} /* parser_stack_iterator_write */
/**
* @}
* @}
*/

View file

@ -0,0 +1,678 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js-parser-internal.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_scanner Scanner
* @{
*/
/**
* Scan mode types types.
*/
typedef enum
{
SCAN_MODE_PRIMARY_EXPRESSION, /**< scanning primary expression */
SCAN_MODE_PRIMARY_EXPRESSION_AFTER_NEW, /**< scanning primary expression after new */
SCAN_MODE_POST_PRIMARY_EXPRESSION, /**< scanning post primary expression */
SCAN_MODE_PRIMARY_EXPRESSION_END, /**< scanning prymary expression end */
SCAN_MODE_STATEMENT, /**< scanning statement */
SCAN_MODE_FUNCTION_ARGUMENTS, /**< scanning function arguments */
SCAN_MODE_PROPERTY_NAME, /**< scanning property name */
} scan_modes_t;
/**
* Scan stack mode types types.
*/
typedef enum
{
SCAN_STACK_HEAD, /**< head */
SCAN_STACK_PAREN_EXPRESSION, /**< parent expression group */
SCAN_STACK_PAREN_STATEMENT, /**< parent stetement group */
SCAN_STACK_COLON_EXPRESSION, /**< colon expression group */
SCAN_STACK_COLON_STATEMENT, /**< colon statement group*/
SCAN_STACK_SQUARE_BRACKETED_EXPRESSION, /**< square bracketed expression group */
SCAN_STACK_OBJECT_LITERAL, /**< object literal group */
SCAN_STACK_BLOCK_STATEMENT, /**< block statement group */
SCAN_STACK_BLOCK_EXPRESSION, /**< block expression group*/
SCAN_STACK_BLOCK_PROPERTY, /**< block property group */
} scan_stack_modes_t;
/**
* Scan primary expression.
*
* @return true for continue, false for break
*/
static bool
parser_scan_primary_expression (parser_context_t *context_p, /**< context */
lexer_token_type_t type, /**< current token type */
scan_stack_modes_t stack_top, /**< current stack top */
scan_modes_t *mode) /**< scan mode */
{
switch (type)
{
case LEXER_KEYW_NEW:
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION_AFTER_NEW;
break;
}
case LEXER_DIVIDE:
case LEXER_ASSIGN_DIVIDE:
{
lexer_construct_regexp_object (context_p, true);
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
}
case LEXER_KEYW_FUNCTION:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_EXPRESSION);
*mode = SCAN_MODE_FUNCTION_ARGUMENTS;
break;
}
case LEXER_LEFT_PAREN:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_EXPRESSION);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
break;
}
case LEXER_LEFT_SQUARE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_SQUARE_BRACKETED_EXPRESSION);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
break;
}
case LEXER_LEFT_BRACE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_OBJECT_LITERAL);
*mode = SCAN_MODE_PROPERTY_NAME;
return true;
}
case LEXER_LITERAL:
case LEXER_KEYW_THIS:
case LEXER_LIT_TRUE:
case LEXER_LIT_FALSE:
case LEXER_LIT_NULL:
{
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
}
case LEXER_RIGHT_SQUARE:
{
if (stack_top != SCAN_STACK_SQUARE_BRACKETED_EXPRESSION)
{
parser_raise_error (context_p, PARSER_ERR_PRIMARY_EXP_EXPECTED);
}
parser_stack_pop_uint8 (context_p);
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
}
case LEXER_COMMA:
{
if (stack_top != SCAN_STACK_SQUARE_BRACKETED_EXPRESSION)
{
parser_raise_error (context_p, PARSER_ERR_PRIMARY_EXP_EXPECTED);
}
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
break;
}
case LEXER_RIGHT_PAREN:
{
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
if (stack_top == SCAN_STACK_PAREN_STATEMENT)
{
*mode = SCAN_MODE_STATEMENT;
}
else if (stack_top != SCAN_STACK_PAREN_EXPRESSION)
{
parser_raise_error (context_p, PARSER_ERR_PRIMARY_EXP_EXPECTED);
}
parser_stack_pop_uint8 (context_p);
break;
}
case LEXER_SEMICOLON:
{
/* Needed by for (;;) statements. */
if (stack_top != SCAN_STACK_PAREN_STATEMENT)
{
parser_raise_error (context_p, PARSER_ERR_PRIMARY_EXP_EXPECTED);
}
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
break;
}
default:
{
parser_raise_error (context_p, PARSER_ERR_PRIMARY_EXP_EXPECTED);
}
}
return false;
} /* parser_scan_primary_expression */
/**
* Scan the tokens after the primary expression.
*
* @return true for break, false for fall through
*/
static bool
parser_scan_post_primary_expression (parser_context_t *context_p, /**< context */
lexer_token_type_t type, /**< current token type */
scan_modes_t *mode) /**< scan mode */
{
switch (type)
{
case LEXER_DOT:
{
lexer_scan_identifier (context_p, false);
return true;
}
case LEXER_LEFT_PAREN:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_EXPRESSION);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
case LEXER_LEFT_SQUARE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_SQUARE_BRACKETED_EXPRESSION);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return true;
}
case LEXER_INCREASE:
case LEXER_DECREASE:
{
if (!context_p->token.was_newline)
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION_END;
return true;
}
/* FALLTHRU */
}
default:
{
break;
}
}
return false;
} /* parser_scan_post_primary_expression */
/**
* Scan the tokens after the primary expression.
*
* @return true for continue, false for break
*/
static bool
parser_scan_primary_expression_end (parser_context_t *context_p, /**< context */
lexer_token_type_t type, /**< current token type */
scan_stack_modes_t stack_top, /**< current stack top */
lexer_token_type_t end_type, /**< terminator token type */
scan_modes_t *mode) /**< scan mode */
{
switch (type)
{
case LEXER_QUESTION_MARK:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_COLON_EXPRESSION);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
case LEXER_COMMA:
{
if (stack_top == SCAN_STACK_OBJECT_LITERAL)
{
*mode = SCAN_MODE_PROPERTY_NAME;
return true;
}
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
case LEXER_COLON:
{
if (stack_top == SCAN_STACK_COLON_EXPRESSION
|| stack_top == SCAN_STACK_COLON_STATEMENT)
{
if (stack_top == SCAN_STACK_COLON_EXPRESSION)
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
}
else
{
*mode = SCAN_MODE_STATEMENT;
}
parser_stack_pop_uint8 (context_p);
return false;
}
/* FALLTHRU */
}
default:
{
break;
}
}
if (LEXER_IS_BINARY_OP_TOKEN (type)
|| (type == LEXER_SEMICOLON && stack_top == SCAN_STACK_PAREN_STATEMENT))
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
if ((type == LEXER_RIGHT_SQUARE && stack_top == SCAN_STACK_SQUARE_BRACKETED_EXPRESSION)
|| (type == LEXER_RIGHT_PAREN && stack_top == SCAN_STACK_PAREN_EXPRESSION)
|| (type == LEXER_RIGHT_BRACE && stack_top == SCAN_STACK_OBJECT_LITERAL))
{
parser_stack_pop_uint8 (context_p);
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
return false;
}
*mode = SCAN_MODE_STATEMENT;
if (type == LEXER_RIGHT_PAREN && stack_top == SCAN_STACK_PAREN_STATEMENT)
{
parser_stack_pop_uint8 (context_p);
return false;
}
/* Check whether we can enter to statement mode. */
if (stack_top != SCAN_STACK_BLOCK_STATEMENT
&& stack_top != SCAN_STACK_BLOCK_EXPRESSION
&& !(stack_top == SCAN_STACK_HEAD && end_type == LEXER_SCAN_SWITCH))
{
parser_raise_error (context_p, PARSER_ERR_INVALID_EXPRESSION);
}
if (type == LEXER_RIGHT_BRACE
|| context_p->token.was_newline)
{
return true;
}
if (type != LEXER_SEMICOLON)
{
parser_raise_error (context_p, PARSER_ERR_INVALID_EXPRESSION);
}
return false;
} /* parser_scan_primary_expression_end */
/**
* Scan statements.
*
* @return true for continue, false for break
*/
static bool
parser_scan_statement (parser_context_t *context_p, /**< context */
lexer_token_type_t type, /**< current token type */
scan_stack_modes_t stack_top, /**< current stack top */
scan_modes_t *mode) /**< scan mode */
{
switch (type)
{
case LEXER_SEMICOLON:
case LEXER_KEYW_ELSE:
case LEXER_KEYW_DO:
case LEXER_KEYW_TRY:
case LEXER_KEYW_FINALLY:
case LEXER_KEYW_DEBUGGER:
{
return false;
}
case LEXER_KEYW_IF:
case LEXER_KEYW_WHILE:
case LEXER_KEYW_WITH:
case LEXER_KEYW_SWITCH:
case LEXER_KEYW_CATCH:
{
lexer_next_token (context_p);
if (context_p->token.type != LEXER_LEFT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_LEFT_PAREN_EXPECTED);
}
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_STATEMENT);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
case LEXER_KEYW_FOR:
{
lexer_next_token (context_p);
if (context_p->token.type != LEXER_LEFT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_LEFT_PAREN_EXPECTED);
}
lexer_next_token (context_p);
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_STATEMENT);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
if (context_p->token.type == LEXER_KEYW_VAR)
{
return false;
}
return true;
}
case LEXER_KEYW_VAR:
case LEXER_KEYW_THROW:
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
case LEXER_KEYW_RETURN:
{
lexer_next_token (context_p);
if (!context_p->token.was_newline
&& context_p->token.type != LEXER_SEMICOLON)
{
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
}
return true;
}
case LEXER_KEYW_BREAK:
case LEXER_KEYW_CONTINUE:
{
lexer_next_token (context_p);
if (!context_p->token.was_newline
&& context_p->token.type == LEXER_LITERAL
&& context_p->token.lit_location.type == LEXER_IDENT_LITERAL)
{
return false;
}
return true;
}
case LEXER_KEYW_DEFAULT:
{
lexer_next_token (context_p);
if (context_p->token.type != LEXER_COLON)
{
parser_raise_error (context_p, PARSER_ERR_COLON_EXPECTED);
}
return false;
}
case LEXER_KEYW_CASE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_COLON_STATEMENT);
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
return false;
}
case LEXER_RIGHT_BRACE:
{
if (stack_top == SCAN_STACK_BLOCK_STATEMENT
|| stack_top == SCAN_STACK_BLOCK_EXPRESSION
|| stack_top == SCAN_STACK_BLOCK_PROPERTY)
{
parser_stack_pop_uint8 (context_p);
if (stack_top == SCAN_STACK_BLOCK_EXPRESSION)
{
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
}
else if (stack_top == SCAN_STACK_BLOCK_PROPERTY)
{
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
lexer_next_token (context_p);
if (context_p->token.type != LEXER_COMMA
&& context_p->token.type != LEXER_RIGHT_BRACE)
{
parser_raise_error (context_p, PARSER_ERR_OBJECT_ITEM_SEPARATOR_EXPECTED);
}
return true;
}
return false;
}
break;
}
case LEXER_LEFT_BRACE:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_STATEMENT);
return false;
}
case LEXER_KEYW_FUNCTION:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_STATEMENT);
*mode = SCAN_MODE_FUNCTION_ARGUMENTS;
return false;
}
default:
{
break;
}
}
*mode = SCAN_MODE_PRIMARY_EXPRESSION;
if (type == LEXER_LITERAL
&& context_p->token.lit_location.type == LEXER_IDENT_LITERAL)
{
lexer_next_token (context_p);
if (context_p->token.type == LEXER_COLON)
{
*mode = SCAN_MODE_STATEMENT;
return false;
}
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
}
return true;
} /* parser_scan_statement */
/**
* Pre-scan for token(s).
*/
void
parser_scan_until (parser_context_t *context_p, /**< context */
lexer_range_t *range_p, /**< destination range */
lexer_token_type_t end_type) /**< terminator token type */
{
scan_modes_t mode;
lexer_token_type_t end_type_b = end_type;
range_p->source_p = context_p->source_p;
range_p->source_end_p = context_p->source_p;
range_p->line = context_p->line;
range_p->column = context_p->column;
mode = SCAN_MODE_PRIMARY_EXPRESSION;
if (end_type == LEXER_KEYW_CASE)
{
end_type = LEXER_SCAN_SWITCH;
end_type_b = LEXER_SCAN_SWITCH;
mode = SCAN_MODE_STATEMENT;
}
else
{
lexer_next_token (context_p);
if (end_type == LEXER_KEYW_IN)
{
end_type_b = LEXER_SEMICOLON;
if (context_p->token.type == LEXER_KEYW_VAR)
{
lexer_next_token (context_p);
}
}
}
parser_stack_push_uint8 (context_p, SCAN_STACK_HEAD);
while (true)
{
lexer_token_type_t type = (lexer_token_type_t) context_p->token.type;
scan_stack_modes_t stack_top = (scan_stack_modes_t) context_p->stack_top_uint8;
if (type == LEXER_EOS)
{
parser_raise_error (context_p, PARSER_ERR_EXPRESSION_EXPECTED);
}
if (stack_top == SCAN_STACK_HEAD
&& (type == end_type || type == end_type_b))
{
parser_stack_pop_uint8 (context_p);
return;
}
switch (mode)
{
case SCAN_MODE_PRIMARY_EXPRESSION:
{
if (type == LEXER_ADD
|| type == LEXER_SUBTRACT
|| LEXER_IS_UNARY_OP_TOKEN (type))
{
break;
}
/* FALLTHRU */
}
case SCAN_MODE_PRIMARY_EXPRESSION_AFTER_NEW:
{
if (parser_scan_primary_expression (context_p, type, stack_top, &mode))
{
continue;
}
break;
}
case SCAN_MODE_POST_PRIMARY_EXPRESSION:
{
if (parser_scan_post_primary_expression (context_p, type, &mode))
{
break;
}
/* FALLTHRU */
}
case SCAN_MODE_PRIMARY_EXPRESSION_END:
{
if (parser_scan_primary_expression_end (context_p, type, stack_top, end_type, &mode))
{
continue;
}
break;
}
case SCAN_MODE_STATEMENT:
{
if (end_type == LEXER_SCAN_SWITCH
&& stack_top == SCAN_STACK_HEAD
&& (type == LEXER_KEYW_DEFAULT || type == LEXER_KEYW_CASE || type == LEXER_RIGHT_BRACE))
{
parser_stack_pop_uint8 (context_p);
return;
}
if (parser_scan_statement (context_p, type, stack_top, &mode))
{
continue;
}
break;
}
case SCAN_MODE_FUNCTION_ARGUMENTS:
{
JERRY_ASSERT (stack_top == SCAN_STACK_BLOCK_STATEMENT
|| stack_top == SCAN_STACK_BLOCK_EXPRESSION
|| stack_top == SCAN_STACK_BLOCK_PROPERTY);
if (context_p->token.type == LEXER_LITERAL
&& context_p->token.lit_location.type == LEXER_IDENT_LITERAL)
{
lexer_next_token (context_p);
}
if (context_p->token.type != LEXER_LEFT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_ARGUMENT_LIST_EXPECTED);
}
lexer_next_token (context_p);
if (context_p->token.type != LEXER_RIGHT_PAREN)
{
while (true)
{
if (context_p->token.type != LEXER_LITERAL
|| context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
{
parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_EXPECTED);
}
lexer_next_token (context_p);
if (context_p->token.type != LEXER_COMMA)
{
break;
}
lexer_next_token (context_p);
}
}
if (context_p->token.type != LEXER_RIGHT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_RIGHT_PAREN_EXPECTED);
}
lexer_next_token (context_p);
if (context_p->token.type != LEXER_LEFT_BRACE)
{
parser_raise_error (context_p, PARSER_ERR_LEFT_BRACE_EXPECTED);
}
mode = SCAN_MODE_STATEMENT;
break;
}
case SCAN_MODE_PROPERTY_NAME:
{
JERRY_ASSERT (stack_top == SCAN_STACK_OBJECT_LITERAL);
lexer_scan_identifier (context_p, true);
if (context_p->token.type == LEXER_RIGHT_BRACE)
{
parser_stack_pop_uint8 (context_p);
mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
break;
}
if (context_p->token.type == LEXER_PROPERTY_GETTER
|| context_p->token.type == LEXER_PROPERTY_SETTER)
{
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_PROPERTY);
mode = SCAN_MODE_FUNCTION_ARGUMENTS;
break;
}
lexer_next_token (context_p);
if (context_p->token.type != LEXER_COLON)
{
parser_raise_error (context_p, PARSER_ERR_COLON_EXPECTED);
}
mode = SCAN_MODE_PRIMARY_EXPRESSION;
break;
}
}
range_p->source_end_p = context_p->source_p;
lexer_next_token (context_p);
}
} /* parser_scan_until */
/**
* @}
* @}
* @}
*/

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,940 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js-parser-internal.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_utils Utility
* @{
*/
/**********************************************************************/
/* Emitting byte codes */
/**********************************************************************/
/**
* Append two bytes to the cbc stream.
*/
static void
parser_emit_two_bytes (parser_context_t *context_p, /**< context */
uint8_t first_byte, /**< first byte */
uint8_t second_byte) /**< second byte */
{
uint32_t last_position = context_p->byte_code.last_position;
if (last_position + 2 <= PARSER_CBC_STREAM_PAGE_SIZE)
{
parser_mem_page_t *page_p = context_p->byte_code.last_p;
page_p->bytes[last_position] = first_byte;
page_p->bytes[last_position + 1] = second_byte;
context_p->byte_code.last_position = last_position + 2;
}
else if (last_position >= PARSER_CBC_STREAM_PAGE_SIZE)
{
parser_mem_page_t *page_p;
parser_cbc_stream_alloc_page (context_p, &context_p->byte_code);
page_p = context_p->byte_code.last_p;
page_p->bytes[0] = first_byte;
page_p->bytes[1] = second_byte;
context_p->byte_code.last_position = 2;
}
else
{
context_p->byte_code.last_p->bytes[PARSER_CBC_STREAM_PAGE_SIZE - 1] = first_byte;
parser_cbc_stream_alloc_page (context_p, &context_p->byte_code);
context_p->byte_code.last_p->bytes[0] = second_byte;
context_p->byte_code.last_position = 1;
}
} /* parser_emit_two_bytes */
#define PARSER_APPEND_TO_BYTE_CODE(context_p, byte) \
if ((context_p)->byte_code.last_position >= PARSER_CBC_STREAM_PAGE_SIZE) \
{ \
parser_cbc_stream_alloc_page ((context_p), &(context_p)->byte_code); \
} \
(context_p)->byte_code.last_p->bytes[(context_p)->byte_code.last_position++] = (uint8_t) (byte)
/**
* Append the current byte code to the stream
*/
void
parser_flush_cbc (parser_context_t *context_p) /**< context */
{
uint8_t flags;
if (context_p->last_cbc_opcode == PARSER_CBC_UNAVAILABLE)
{
return;
}
context_p->status_flags |= PARSER_NO_END_LABEL;
if (PARSER_IS_BASIC_OPCODE (context_p->last_cbc_opcode))
{
cbc_opcode_t opcode = (cbc_opcode_t) context_p->last_cbc_opcode;
flags = cbc_flags[opcode];
PARSER_APPEND_TO_BYTE_CODE (context_p, opcode);
context_p->byte_code_size++;
}
else
{
cbc_ext_opcode_t opcode = (cbc_ext_opcode_t) PARSER_GET_EXT_OPCODE (context_p->last_cbc_opcode);
flags = cbc_ext_flags[opcode];
parser_emit_two_bytes (context_p, CBC_EXT_OPCODE, opcode);
context_p->byte_code_size += 2;
}
JERRY_ASSERT ((flags >> CBC_STACK_ADJUST_SHIFT) >= CBC_STACK_ADJUST_BASE
|| (CBC_STACK_ADJUST_BASE - (flags >> CBC_STACK_ADJUST_SHIFT)) <= context_p->stack_depth);
PARSER_PLUS_EQUAL_U16 (context_p->stack_depth, CBC_STACK_ADJUST_VALUE (flags));
if (flags & (CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2))
{
uint16_t literal_index = context_p->last_cbc.literal_index;
parser_emit_two_bytes (context_p,
(uint8_t) (literal_index & 0xff),
(uint8_t) (literal_index >> 8));
context_p->byte_code_size += 2;
}
if (flags & CBC_HAS_LITERAL_ARG2)
{
uint16_t literal_index = context_p->last_cbc.value;
parser_emit_two_bytes (context_p,
(uint8_t) (literal_index & 0xff),
(uint8_t) (literal_index >> 8));
context_p->byte_code_size += 2;
if (!(flags & CBC_HAS_LITERAL_ARG))
{
literal_index = context_p->last_cbc.third_literal_index;
parser_emit_two_bytes (context_p,
(uint8_t) (literal_index & 0xff),
(uint8_t) (literal_index >> 8));
context_p->byte_code_size += 2;
}
}
if (flags & CBC_HAS_BYTE_ARG)
{
uint8_t byte_argument = (uint8_t) context_p->last_cbc.value;
JERRY_ASSERT (context_p->last_cbc.value <= CBC_MAXIMUM_BYTE_VALUE);
if (flags & CBC_POP_STACK_BYTE_ARG)
{
JERRY_ASSERT (context_p->stack_depth >= byte_argument);
PARSER_MINUS_EQUAL_U16 (context_p->stack_depth, byte_argument);
}
PARSER_APPEND_TO_BYTE_CODE (context_p, byte_argument);
context_p->byte_code_size++;
}
#ifdef PARSER_DUMP_BYTE_CODE
if (context_p->is_show_opcodes)
{
const char *name_p;
if (PARSER_IS_BASIC_OPCODE (context_p->last_cbc_opcode))
{
name_p = cbc_names[context_p->last_cbc_opcode];
}
else
{
name_p = cbc_ext_names[PARSER_GET_EXT_OPCODE (context_p->last_cbc_opcode)];
}
JERRY_DEBUG_MSG (" [%3d] %s", (int) context_p->stack_depth, name_p);
if (flags & (CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2))
{
uint16_t literal_index = context_p->last_cbc.literal_index;
lexer_literal_t *literal_p = PARSER_GET_LITERAL (literal_index);
JERRY_DEBUG_MSG (" idx:%d->", literal_index);
util_print_literal (literal_p);
}
if (flags & CBC_HAS_LITERAL_ARG2)
{
uint16_t literal_index = context_p->last_cbc.value;
lexer_literal_t *literal_p = PARSER_GET_LITERAL (literal_index);
JERRY_DEBUG_MSG (" idx:%d->", literal_index);
util_print_literal (literal_p);
if (!(flags & CBC_HAS_LITERAL_ARG))
{
literal_index = context_p->last_cbc.third_literal_index;
lexer_literal_t *literal_p = PARSER_GET_LITERAL (literal_index);
JERRY_DEBUG_MSG (" idx:%d->", literal_index);
util_print_literal (literal_p);
}
}
if (flags & CBC_HAS_BYTE_ARG)
{
JERRY_DEBUG_MSG (" byte_arg:%d", (int) context_p->last_cbc.value);
}
JERRY_DEBUG_MSG ("\n");
}
#endif /* PARSER_DUMP_BYTE_CODE */
if (context_p->stack_depth > context_p->stack_limit)
{
context_p->stack_limit = context_p->stack_depth;
if (context_p->stack_limit > PARSER_MAXIMUM_STACK_LIMIT)
{
parser_raise_error (context_p, PARSER_ERR_STACK_LIMIT_REACHED);
}
}
context_p->last_cbc_opcode = PARSER_CBC_UNAVAILABLE;
} /* parser_flush_cbc */
/**
* Append a byte code
*/
void
parser_emit_cbc (parser_context_t *context_p, /**< context */
uint16_t opcode) /**< opcode */
{
JERRY_ASSERT (PARSER_ARGS_EQ (opcode, 0));
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->last_cbc_opcode = opcode;
} /* parser_emit_cbc */
/**
* Append a byte code with a literal argument
*/
void
parser_emit_cbc_literal (parser_context_t *context_p, /**< context */
uint16_t opcode, /**< opcode */
uint16_t literal_index) /**< literal index */
{
JERRY_ASSERT (PARSER_ARGS_EQ (opcode, CBC_HAS_LITERAL_ARG));
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->last_cbc_opcode = opcode;
context_p->last_cbc.literal_index = literal_index;
context_p->last_cbc.literal_type = LEXER_UNUSED_LITERAL;
context_p->last_cbc.literal_object_type = LEXER_LITERAL_OBJECT_ANY;
} /* parser_emit_cbc_literal */
/**
* Append a byte code with the current literal argument
*/
void
parser_emit_cbc_literal_from_token (parser_context_t *context_p, /**< context */
uint16_t opcode) /**< opcode */
{
JERRY_ASSERT (PARSER_ARGS_EQ (opcode, CBC_HAS_LITERAL_ARG));
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->last_cbc_opcode = opcode;
context_p->last_cbc.literal_index = context_p->lit_object.index;
context_p->last_cbc.literal_type = context_p->token.lit_location.type;
context_p->last_cbc.literal_object_type = context_p->lit_object.type;
} /* parser_emit_cbc_literal_from_token */
/**
* Append a byte code with a call argument
*/
void
parser_emit_cbc_call (parser_context_t *context_p, /**< context */
uint16_t opcode, /**< opcode */
size_t call_arguments) /**< number of arguments */
{
JERRY_ASSERT (PARSER_ARGS_EQ (opcode, CBC_HAS_BYTE_ARG));
JERRY_ASSERT (call_arguments <= CBC_MAXIMUM_BYTE_VALUE);
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->last_cbc_opcode = opcode;
context_p->last_cbc.value = (uint16_t) call_arguments;
} /* parser_emit_cbc_call */
/**
* Append a push number 1/2 byte code
*/
void
parser_emit_cbc_push_number (parser_context_t *context_p, /**< context */
bool is_negative_number) /**< sign is negative */
{
uint16_t value = context_p->lit_object.index;
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
cbc_opcode_t opcode = is_negative_number ? CBC_PUSH_NUMBER_NEG_BYTE : CBC_PUSH_NUMBER_POS_BYTE;
JERRY_ASSERT (value > 0 && value <= CBC_PUSH_NUMBER_BYTE_RANGE_END);
JERRY_ASSERT (CBC_STACK_ADJUST_VALUE (cbc_flags[opcode]) == 1);
context_p->stack_depth++;
#ifdef PARSER_DUMP_BYTE_CODE
if (context_p->is_show_opcodes)
{
int real_value = value;
if (is_negative_number)
{
real_value = -real_value;
}
JERRY_DEBUG_MSG (" [%3d] %s number:%d\n",
(int) context_p->stack_depth,
cbc_names[opcode],
real_value);
}
#endif /* PARSER_DUMP_BYTE_CODE */
parser_emit_two_bytes (context_p, opcode, (uint8_t) (value - 1));
context_p->byte_code_size += 2;
if (context_p->stack_depth > context_p->stack_limit)
{
context_p->stack_limit = context_p->stack_depth;
if (context_p->stack_limit > PARSER_MAXIMUM_STACK_LIMIT)
{
parser_raise_error (context_p, PARSER_ERR_STACK_LIMIT_REACHED);
}
}
} /* parser_emit_cbc_push_number */
/**
* Append a byte code with a branch argument
*/
void
parser_emit_cbc_forward_branch (parser_context_t *context_p, /**< context */
uint16_t opcode, /**< opcode */
parser_branch_t *branch_p) /**< branch result */
{
uint8_t flags;
uint32_t extra_byte_code_increase;
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->status_flags |= PARSER_NO_END_LABEL;
if (PARSER_IS_BASIC_OPCODE (opcode))
{
flags = cbc_flags[opcode];
extra_byte_code_increase = 0;
}
else
{
PARSER_APPEND_TO_BYTE_CODE (context_p, CBC_EXT_OPCODE);
opcode = (uint16_t) PARSER_GET_EXT_OPCODE (opcode);
flags = cbc_ext_flags[opcode];
extra_byte_code_increase = 1;
}
JERRY_ASSERT (flags & CBC_HAS_BRANCH_ARG);
JERRY_ASSERT (CBC_BRANCH_IS_FORWARD (flags));
JERRY_ASSERT (CBC_BRANCH_OFFSET_LENGTH (opcode) == 1);
/* Branch opcodes never push anything onto the stack. */
JERRY_ASSERT ((flags >> CBC_STACK_ADJUST_SHIFT) >= CBC_STACK_ADJUST_BASE
|| (CBC_STACK_ADJUST_BASE - (flags >> CBC_STACK_ADJUST_SHIFT)) <= context_p->stack_depth);
PARSER_PLUS_EQUAL_U16 (context_p->stack_depth, CBC_STACK_ADJUST_VALUE (flags));
#ifdef PARSER_DUMP_BYTE_CODE
if (context_p->is_show_opcodes)
{
if (extra_byte_code_increase == 0)
{
JERRY_DEBUG_MSG (" [%3d] %s\n", (int) context_p->stack_depth, cbc_names[opcode]);
}
else
{
JERRY_DEBUG_MSG (" [%3d] %s\n", (int) context_p->stack_depth, cbc_ext_names[opcode]);
}
}
#endif /* PARSER_DUMP_BYTE_CODE */
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
opcode++;
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
PARSER_PLUS_EQUAL_U16 (opcode, 2);
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
parser_emit_two_bytes (context_p, (uint8_t) opcode, 0);
branch_p->page_p = context_p->byte_code.last_p;
branch_p->offset = (context_p->byte_code.last_position - 1) | (context_p->byte_code_size << 8);
context_p->byte_code_size += extra_byte_code_increase;
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
PARSER_APPEND_TO_BYTE_CODE (context_p, 0);
context_p->byte_code_size += 3;
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
parser_emit_two_bytes (context_p, 0, 0);
context_p->byte_code_size += 4;
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
if (context_p->stack_depth > context_p->stack_limit)
{
context_p->stack_limit = context_p->stack_depth;
if (context_p->stack_limit > PARSER_MAXIMUM_STACK_LIMIT)
{
parser_raise_error (context_p, PARSER_ERR_STACK_LIMIT_REACHED);
}
}
} /* parser_emit_cbc_forward_branch */
/**
* Append a branch byte code and create an item
*/
parser_branch_node_t *
parser_emit_cbc_forward_branch_item (parser_context_t *context_p, /**< context */
uint16_t opcode, /**< opcode */
parser_branch_node_t *next_p) /**< next branch */
{
parser_branch_t branch;
parser_branch_node_t *new_item;
/* Since byte code insertion may throw an out-of-memory error,
* the branch is constructed locally, and copied later. */
parser_emit_cbc_forward_branch (context_p, opcode, &branch);
new_item = (parser_branch_node_t *) parser_malloc (context_p, sizeof (parser_branch_node_t));
new_item->branch = branch;
new_item->next_p = next_p;
return new_item;
} /* parser_emit_cbc_forward_branch_item */
/**
* Append a byte code with a branch argument
*/
void
parser_emit_cbc_backward_branch (parser_context_t *context_p, /**< context */
uint16_t opcode, /**< opcode */
uint32_t offset) /**< destination offset */
{
uint8_t flags;
#ifdef PARSER_DUMP_BYTE_CODE
const char *name;
#endif /* PARSER_DUMP_BYTE_CODE */
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->status_flags |= PARSER_NO_END_LABEL;
offset = context_p->byte_code_size - offset;
if (PARSER_IS_BASIC_OPCODE (opcode))
{
flags = cbc_flags[opcode];
#ifdef PARSER_DUMP_BYTE_CODE
name = cbc_names[opcode];
#endif /* PARSER_DUMP_BYTE_CODE */
}
else
{
PARSER_APPEND_TO_BYTE_CODE (context_p, CBC_EXT_OPCODE);
opcode = (uint16_t) PARSER_GET_EXT_OPCODE (opcode);
flags = cbc_ext_flags[opcode];
context_p->byte_code_size++;
#ifdef PARSER_DUMP_BYTE_CODE
name = cbc_ext_names[opcode];
#endif /* PARSER_DUMP_BYTE_CODE */
}
JERRY_ASSERT (flags & CBC_HAS_BRANCH_ARG);
JERRY_ASSERT (CBC_BRANCH_IS_BACKWARD (flags));
JERRY_ASSERT (CBC_BRANCH_OFFSET_LENGTH (opcode) == 1);
JERRY_ASSERT (offset <= context_p->byte_code_size);
/* Branch opcodes never push anything onto the stack. */
JERRY_ASSERT ((flags >> CBC_STACK_ADJUST_SHIFT) >= CBC_STACK_ADJUST_BASE
|| (CBC_STACK_ADJUST_BASE - (flags >> CBC_STACK_ADJUST_SHIFT)) <= context_p->stack_depth);
PARSER_PLUS_EQUAL_U16 (context_p->stack_depth, CBC_STACK_ADJUST_VALUE (flags));
#ifdef PARSER_DUMP_BYTE_CODE
if (context_p->is_show_opcodes)
{
JERRY_DEBUG_MSG (" [%3d] %s\n", (int) context_p->stack_depth, name);
}
#endif /* PARSER_DUMP_BYTE_CODE */
context_p->byte_code_size += 2;
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
if (offset > 255)
{
opcode++;
context_p->byte_code_size++;
}
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
if (offset > 65535)
{
PARSER_PLUS_EQUAL_U16 (opcode, 2);
context_p->byte_code_size += 2;
}
else if (offset > 255)
{
opcode++;
context_p->byte_code_size++;
}
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
PARSER_APPEND_TO_BYTE_CODE (context_p, (uint8_t) opcode);
#if PARSER_MAXIMUM_CODE_SIZE > 65535
if (offset > 65535)
{
PARSER_APPEND_TO_BYTE_CODE (context_p, offset >> 16);
}
#endif /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
if (offset > 255)
{
PARSER_APPEND_TO_BYTE_CODE (context_p, (offset >> 8) & 0xff);
}
PARSER_APPEND_TO_BYTE_CODE (context_p, offset & 0xff);
} /* parser_emit_cbc_backward_branch */
#undef PARSER_CHECK_LAST_POSITION
#undef PARSER_APPEND_TO_BYTE_CODE
/**
* Set a branch to the current byte code position
*/
void
parser_set_branch_to_current_position (parser_context_t *context_p, /**< context */
parser_branch_t *branch_p) /**< branch result */
{
uint32_t delta;
size_t offset;
parser_mem_page_t *page_p = branch_p->page_p;
if (context_p->last_cbc_opcode != PARSER_CBC_UNAVAILABLE)
{
parser_flush_cbc (context_p);
}
context_p->status_flags &= ~PARSER_NO_END_LABEL;
JERRY_ASSERT (context_p->byte_code_size > (branch_p->offset >> 8));
delta = context_p->byte_code_size - (branch_p->offset >> 8);
offset = (branch_p->offset & CBC_LOWER_SEVEN_BIT_MASK);
JERRY_ASSERT (delta <= PARSER_MAXIMUM_CODE_SIZE);
#if PARSER_MAXIMUM_CODE_SIZE <= 65535
page_p->bytes[offset++] = (uint8_t) (delta >> 8);
if (offset >= PARSER_CBC_STREAM_PAGE_SIZE)
{
page_p = page_p->next_p;
offset = 0;
}
#else /* PARSER_MAXIMUM_CODE_SIZE > 65535 */
page_p->bytes[offset++] = (uint8_t) (delta >> 16);
if (offset >= PARSER_CBC_STREAM_PAGE_SIZE)
{
page_p = page_p->next_p;
offset = 0;
}
page_p->bytes[offset++] = (uint8_t) ((delta >> 8) & 0xff);
if (offset >= PARSER_CBC_STREAM_PAGE_SIZE)
{
page_p = page_p->next_p;
offset = 0;
}
#endif /* PARSER_MAXIMUM_CODE_SIZE <= 65535 */
page_p->bytes[offset++] = delta & 0xff;
} /* parser_set_branch_to_current_position */
/**
* Set breaks to the current byte code position
*/
void
parser_set_breaks_to_current_position (parser_context_t *context_p, /**< context */
parser_branch_node_t *current_p) /**< branch list */
{
while (current_p != NULL)
{
parser_branch_node_t *next_p = current_p->next_p;
if (!(current_p->branch.offset & CBC_HIGHEST_BIT_MASK))
{
parser_set_branch_to_current_position (context_p, &current_p->branch);
}
parser_free (current_p, sizeof (parser_branch_node_t));
current_p = next_p;
}
} /* parser_set_breaks_to_current_position */
/**
* Set continues to the current byte code position
*/
void
parser_set_continues_to_current_position (parser_context_t *context_p, /**< context */
parser_branch_node_t *current_p) /**< branch list */
{
while (current_p != NULL)
{
if (current_p->branch.offset & CBC_HIGHEST_BIT_MASK)
{
parser_set_branch_to_current_position (context_p, &current_p->branch);
}
current_p = current_p->next_p;
}
} /* parser_set_continues_to_current_position */
#if JERRY_ENABLE_ERROR_MESSAGES
/**
* Returns with the string representation of the error
*/
const char *
parser_error_to_string (parser_error_t error) /**< error code */
{
switch (error)
{
case PARSER_ERR_OUT_OF_MEMORY:
{
return "Out of memory.";
}
case PARSER_ERR_LITERAL_LIMIT_REACHED:
{
return "Maximum number of literals reached.";
}
case PARSER_ERR_ARGUMENT_LIMIT_REACHED:
{
return "Maximum number of function arguments reached.";
}
case PARSER_ERR_STACK_LIMIT_REACHED:
{
return "Maximum function stack size reached.";
}
case PARSER_ERR_REGISTER_LIMIT_REACHED:
{
return "Maximum number of registers is reached.";
}
case PARSER_ERR_INVALID_CHARACTER:
{
return "Invalid (unexpected) character.";
}
case PARSER_ERR_INVALID_HEX_DIGIT:
{
return "Invalid hexadecimal digit.";
}
case PARSER_ERR_INVALID_ESCAPE_SEQUENCE:
{
return "Invalid escape sequence.";
}
case PARSER_ERR_INVALID_UNICODE_ESCAPE_SEQUENCE:
{
return "Invalid unicode escape sequence.";
}
case PARSER_ERR_INVALID_IDENTIFIER_START:
{
return "Character cannot be start of an identifier.";
}
case PARSER_ERR_INVALID_IDENTIFIER_PART:
{
return "Character cannot be part of an identifier.";
}
case PARSER_ERR_INVALID_NUMBER:
{
return "Invalid number.";
}
case PARSER_ERR_MISSING_EXPONENT:
{
return "Missing exponent part.";
}
case PARSER_ERR_IDENTIFIER_AFTER_NUMBER:
{
return "Identifier cannot start after a number.";
}
case PARSER_ERR_INVALID_REGEXP:
{
return "Invalid regular expression.";
}
case PARSER_ERR_UNKNOWN_REGEXP_FLAG:
{
return "Unknown regexp flag.";
}
case PARSER_ERR_DUPLICATED_REGEXP_FLAG:
{
return "Duplicated regexp flag.";
}
case PARSER_ERR_UNSUPPORTED_REGEXP:
{
return "Regexp is not supported in the selected profile.";
}
case PARSER_ERR_IDENTIFIER_TOO_LONG:
{
return "Identifier is too long.";
}
case PARSER_ERR_STRING_TOO_LONG:
{
return "String is too long.";
}
case PARSER_ERR_NUMBER_TOO_LONG:
{
return "Number too long.";
}
case PARSER_ERR_REGEXP_TOO_LONG:
{
return "Regexp too long.";
}
case PARSER_ERR_UNTERMINATED_MULTILINE_COMMENT:
{
return "Unterminated multiline comment.";
}
case PARSER_ERR_UNTERMINATED_STRING:
{
return "Unterminated string literal.";
}
case PARSER_ERR_UNTERMINATED_REGEXP:
{
return "Unterminated regexp literal.";
}
case PARSER_ERR_NEWLINE_NOT_ALLOWED:
{
return "Newline is not allowed in strings or regexps.";
}
case PARSER_ERR_OCTAL_NUMBER_NOT_ALLOWED:
{
return "Octal numbers are not allowed in strict mode.";
}
case PARSER_ERR_OCTAL_ESCAPE_NOT_ALLOWED:
{
return "Octal escape sequences are not allowed in strict mode.";
}
case PARSER_ERR_STRICT_IDENT_NOT_ALLOWED:
{
return "Identifier name is reserved in strict mode.";
}
case PARSER_ERR_EVAL_NOT_ALLOWED:
{
return "Eval is not allowed to use here in strict mode.";
}
case PARSER_ERR_ARGUMENTS_NOT_ALLOWED:
{
return "Arguments is not allowed to use here in strict mode.";
}
case PARSER_ERR_DELETE_IDENT_NOT_ALLOWED:
{
return "Deleting identifier is not allowed in strict mode.";
}
case PARSER_ERR_EVAL_CANNOT_ASSIGNED:
{
return "Eval cannot assigned in strict mode.";
}
case PARSER_ERR_ARGUMENTS_CANNOT_ASSIGNED:
{
return "Arguments cannot assigned in strict mode.";
}
case PARSER_ERR_WITH_NOT_ALLOWED:
{
return "With statement not allowed in strict mode.";
}
case PARSER_ERR_MULTIPLE_DEFAULTS_NOT_ALLOWED:
{
return "Multiple default cases not allowed.";
}
case PARSER_ERR_DEFAULT_NOT_IN_SWITCH:
{
return "Default statement must be in a switch block.";
}
case PARSER_ERR_CASE_NOT_IN_SWITCH:
{
return "Case statement must be in a switch block.";
}
case PARSER_ERR_LEFT_PAREN_EXPECTED:
{
return "Expected '(' token.";
}
case PARSER_ERR_LEFT_BRACE_EXPECTED:
{
return "Expected '{' token.";
}
case PARSER_ERR_RIGHT_PAREN_EXPECTED:
{
return "Expected ')' token.";
}
case PARSER_ERR_RIGHT_SQUARE_EXPECTED:
{
return "Expected ']' token.";
}
case PARSER_ERR_COLON_EXPECTED:
{
return "Expected ':' token.";
}
case PARSER_ERR_COLON_FOR_CONDITIONAL_EXPECTED:
{
return "Expected ':' token for ?: conditional expression.";
}
case PARSER_ERR_SEMICOLON_EXPECTED:
{
return "Expected ';' token.";
}
case PARSER_ERR_IN_EXPECTED:
{
return "Expected 'in' token.";
}
case PARSER_ERR_WHILE_EXPECTED:
{
return "While expected for do-while loop.";
}
case PARSER_ERR_CATCH_FINALLY_EXPECTED:
{
return "Catch or finally block expected.";
}
case PARSER_ERR_ARRAY_ITEM_SEPARATOR_EXPECTED:
{
return "Expected ',' or ']' after an array item.";
}
case PARSER_ERR_OBJECT_ITEM_SEPARATOR_EXPECTED:
{
return "Expected ',' or '}' after a property definition.";
}
case PARSER_ERR_IDENTIFIER_EXPECTED:
{
return "Identifier expected.";
}
case PARSER_ERR_EXPRESSION_EXPECTED:
{
return "Expression expected.";
}
case PARSER_ERR_PRIMARY_EXP_EXPECTED:
{
return "Primary expression expected.";
}
case PARSER_ERR_STATEMENT_EXPECTED:
{
return "Statement expected.";
}
case PARSER_ERR_PROPERTY_IDENTIFIER_EXPECTED:
{
return "Property identifier expected.";
}
case PARSER_ERR_ARGUMENT_LIST_EXPECTED:
{
return "Expected argument list.";
}
case PARSER_ERR_NO_ARGUMENTS_EXPECTED:
{
return "Property getters must have no arguments.";
}
case PARSER_ERR_ONE_ARGUMENT_EXPECTED:
{
return "Property setters must have one argument.";
}
case PARSER_ERR_INVALID_EXPRESSION:
{
return "Invalid expression.";
}
case PARSER_ERR_INVALID_SWITCH:
{
return "Invalid switch body.";
}
case PARSER_ERR_INVALID_BREAK:
{
return "Break statement must be inside a loop or switch.";
}
case PARSER_ERR_INVALID_BREAK_LABEL:
{
return "Labelled statement targeted by a break not found.";
}
case PARSER_ERR_INVALID_CONTINUE:
{
return "Continue statement must be inside a loop.";
}
case PARSER_ERR_INVALID_CONTINUE_LABEL:
{
return "Labelled statement targeted by a continue noty found.";
}
case PARSER_ERR_INVALID_RETURN:
{
return "Return statement must be inside a function body.";
}
case PARSER_ERR_INVALID_RIGHT_SQUARE:
{
return "Unexpected '}' token.";
}
case PARSER_ERR_DUPLICATED_LABEL:
{
return "Duplicated label.";
}
case PARSER_ERR_OBJECT_PROPERTY_REDEFINED:
{
return "Property of object literal redefined.";
}
case PARSER_ERR_NON_STRICT_ARG_DEFINITION:
{
return "Non strict argument definition.";
}
default:
{
JERRY_ASSERT (error == PARSER_ERR_NO_ERROR);
return "No error.";
}
}
} /* parser_error_to_string */
#endif /* JERRY_ENABLE_ERROR_MESSAGES */
/**
* @}
* @}
* @}
*/

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,142 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef JS_PARSER_H
#define JS_PARSER_H
#include "ecma-globals.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup jsparser JavaScript
* @{
*
* \addtogroup jsparser_parser Parser
* @{
*/
/**
* Error codes.
*/
typedef enum
{
PARSER_ERR_NO_ERROR, /**< no error */
PARSER_ERR_OUT_OF_MEMORY, /**< out of memory */
PARSER_ERR_LITERAL_LIMIT_REACHED, /**< maximum number of literals reached */
PARSER_ERR_ARGUMENT_LIMIT_REACHED, /**< maximum number of function arguments reached */
PARSER_ERR_STACK_LIMIT_REACHED, /**< maximum function stack size reached */
PARSER_ERR_REGISTER_LIMIT_REACHED, /**< maximum register size reached */
PARSER_ERR_INVALID_CHARACTER, /**< unexpected character */
PARSER_ERR_INVALID_HEX_DIGIT, /**< invalid hexadecimal digit */
PARSER_ERR_INVALID_ESCAPE_SEQUENCE, /**< invalid escape sequence */
PARSER_ERR_INVALID_UNICODE_ESCAPE_SEQUENCE, /**< invalid unicode escape sequence */
PARSER_ERR_INVALID_IDENTIFIER_START, /**< character cannot be start of an identifier */
PARSER_ERR_INVALID_IDENTIFIER_PART, /**< character cannot be part of an identifier */
PARSER_ERR_INVALID_NUMBER, /**< invalid number literal */
PARSER_ERR_MISSING_EXPONENT, /**< missing exponent */
PARSER_ERR_IDENTIFIER_AFTER_NUMBER, /**< identifier start after number */
PARSER_ERR_INVALID_REGEXP, /**< invalid regular expression */
PARSER_ERR_UNKNOWN_REGEXP_FLAG, /**< unknown regexp flag */
PARSER_ERR_DUPLICATED_REGEXP_FLAG, /**< duplicated regexp flag */
PARSER_ERR_UNSUPPORTED_REGEXP, /**< regular expression is not supported */
PARSER_ERR_IDENTIFIER_TOO_LONG, /**< too long identifier */
PARSER_ERR_STRING_TOO_LONG, /**< too long string literal */
PARSER_ERR_NUMBER_TOO_LONG, /**< too long number literal */
PARSER_ERR_REGEXP_TOO_LONG, /**< too long regexp literal */
PARSER_ERR_UNTERMINATED_MULTILINE_COMMENT, /**< unterminated multiline comment */
PARSER_ERR_UNTERMINATED_STRING, /**< unterminated string literal */
PARSER_ERR_UNTERMINATED_REGEXP, /**< unterminated regexp literal */
PARSER_ERR_NEWLINE_NOT_ALLOWED, /**< newline is not allowed */
PARSER_ERR_OCTAL_NUMBER_NOT_ALLOWED, /**< octal numbers are not allowed in strict mode */
PARSER_ERR_OCTAL_ESCAPE_NOT_ALLOWED, /**< octal escape sequences are not allowed in strict mode */
PARSER_ERR_STRICT_IDENT_NOT_ALLOWED, /**< identifier name is reserved in strict mode */
PARSER_ERR_EVAL_NOT_ALLOWED, /**< eval is not allowed here in strict mode */
PARSER_ERR_ARGUMENTS_NOT_ALLOWED, /**< arguments is not allowed here in strict mode */
PARSER_ERR_DELETE_IDENT_NOT_ALLOWED, /**< identifier delete is not allowed in strict mode */
PARSER_ERR_EVAL_CANNOT_ASSIGNED, /**< eval cannot be assigned in strict mode */
PARSER_ERR_ARGUMENTS_CANNOT_ASSIGNED, /**< arguments cannot be assigned in strict mode */
PARSER_ERR_WITH_NOT_ALLOWED, /**< with statement is not allowed in strict mode */
PARSER_ERR_MULTIPLE_DEFAULTS_NOT_ALLOWED, /**< multiple default cases are not allowed */
PARSER_ERR_DEFAULT_NOT_IN_SWITCH, /**< default statement is not in switch block */
PARSER_ERR_CASE_NOT_IN_SWITCH, /**< case statement is not in switch block */
PARSER_ERR_LEFT_PAREN_EXPECTED, /**< left paren expected */
PARSER_ERR_LEFT_BRACE_EXPECTED, /**< left brace expected */
PARSER_ERR_RIGHT_PAREN_EXPECTED, /**< right paren expected */
PARSER_ERR_RIGHT_SQUARE_EXPECTED, /**< right square expected */
PARSER_ERR_COLON_EXPECTED, /**< colon expected */
PARSER_ERR_COLON_FOR_CONDITIONAL_EXPECTED, /**< colon expected for conditional expression */
PARSER_ERR_SEMICOLON_EXPECTED, /**< semicolon expected */
PARSER_ERR_IN_EXPECTED, /**< in keyword expected */
PARSER_ERR_WHILE_EXPECTED, /**< while expected for do-while loop */
PARSER_ERR_CATCH_FINALLY_EXPECTED, /**< catch or finally expected */
PARSER_ERR_ARRAY_ITEM_SEPARATOR_EXPECTED, /**< array item separator expected */
PARSER_ERR_OBJECT_ITEM_SEPARATOR_EXPECTED, /**< object item separator expected */
PARSER_ERR_IDENTIFIER_EXPECTED, /**< identifier expected */
PARSER_ERR_EXPRESSION_EXPECTED, /**< expression expected */
PARSER_ERR_PRIMARY_EXP_EXPECTED, /**< primary expression expected */
PARSER_ERR_STATEMENT_EXPECTED, /**< statement expected */
PARSER_ERR_PROPERTY_IDENTIFIER_EXPECTED, /**< property identifier expected */
PARSER_ERR_ARGUMENT_LIST_EXPECTED, /**< argument list expected */
PARSER_ERR_NO_ARGUMENTS_EXPECTED, /**< property getters must have no arguments */
PARSER_ERR_ONE_ARGUMENT_EXPECTED, /**< property setters must have one argument */
PARSER_ERR_INVALID_EXPRESSION, /**< invalid expression */
PARSER_ERR_INVALID_SWITCH, /**< invalid switch body */
PARSER_ERR_INVALID_BREAK, /**< break must be inside a loop or switch */
PARSER_ERR_INVALID_BREAK_LABEL, /**< break target not found */
PARSER_ERR_INVALID_CONTINUE, /**< continue must be inside a loop */
PARSER_ERR_INVALID_CONTINUE_LABEL, /**< continue target not found */
PARSER_ERR_INVALID_RETURN, /**< return must be inside a function */
PARSER_ERR_INVALID_RIGHT_SQUARE, /**< right square must terminate a block */
PARSER_ERR_DUPLICATED_LABEL, /**< duplicated label */
PARSER_ERR_OBJECT_PROPERTY_REDEFINED, /**< property of object literal redefined */
PARSER_ERR_NON_STRICT_ARG_DEFINITION /**< non-strict argument definition */
} parser_error_t;
/* Source code line counter type. */
typedef uint32_t parser_line_counter_t;
/**
* Error code location.
*/
typedef struct
{
parser_error_t error; /**< error code */
parser_line_counter_t line; /**< line where the error occured */
parser_line_counter_t column; /**< column where the error occured */
} parser_error_location_t;
/* Note: source must be a valid UTF-8 string */
extern ecma_value_t parser_parse_script (const uint8_t *, size_t, bool, ecma_compiled_code_t **);
const char *parser_error_to_string (parser_error_t);
/**
* @}
* @}
* @}
*/
#endif /* !JS_PARSER_H */

View file

@ -0,0 +1,445 @@
/* Copyright 2016 Samsung Electronics Co., Ltd.
* Copyright 2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-globals.h"
#include "re-bytecode.h"
#ifndef CONFIG_DISABLE_REGEXP_BUILTIN
/** \addtogroup parser Parser
* @{
*
* \addtogroup regexparser Regular expression
* @{
*
* \addtogroup regexparser_bytecode Bytecode
* @{
*/
/**
* Size of block of RegExp bytecode. Used for allocation
*/
#define REGEXP_BYTECODE_BLOCK_SIZE 256UL
/**
* Realloc the bytecode container
*
* @return current position in RegExp bytecode
*/
static uint8_t *
re_realloc_regexp_bytecode_block (re_bytecode_ctx_t *bc_ctx_p) /**< RegExp bytecode context */
{
JERRY_ASSERT (bc_ctx_p->block_end_p >= bc_ctx_p->block_start_p);
size_t old_size = (size_t) (bc_ctx_p->block_end_p - bc_ctx_p->block_start_p);
/* If one of the members of RegExp bytecode context is NULL, then all member should be NULL
* (it means first allocation), otherwise all of the members should be a non NULL pointer. */
JERRY_ASSERT ((!bc_ctx_p->current_p && !bc_ctx_p->block_end_p && !bc_ctx_p->block_start_p)
|| (bc_ctx_p->current_p && bc_ctx_p->block_end_p && bc_ctx_p->block_start_p));
size_t new_block_size = old_size + REGEXP_BYTECODE_BLOCK_SIZE;
JERRY_ASSERT (bc_ctx_p->current_p >= bc_ctx_p->block_start_p);
size_t current_ptr_offset = (size_t) (bc_ctx_p->current_p - bc_ctx_p->block_start_p);
uint8_t *new_block_start_p = (uint8_t *) jmem_heap_alloc_block (new_block_size);
if (bc_ctx_p->current_p)
{
memcpy (new_block_start_p, bc_ctx_p->block_start_p, (size_t) (current_ptr_offset));
jmem_heap_free_block (bc_ctx_p->block_start_p, old_size);
}
bc_ctx_p->block_start_p = new_block_start_p;
bc_ctx_p->block_end_p = new_block_start_p + new_block_size;
bc_ctx_p->current_p = new_block_start_p + current_ptr_offset;
return bc_ctx_p->current_p;
} /* re_realloc_regexp_bytecode_block */
/**
* Append a new bytecode to the and of the bytecode container
*/
static void
re_bytecode_list_append (re_bytecode_ctx_t *bc_ctx_p, /**< RegExp bytecode context */
uint8_t *bytecode_p, /**< input bytecode */
size_t length) /**< length of input */
{
JERRY_ASSERT (length <= REGEXP_BYTECODE_BLOCK_SIZE);
uint8_t *current_p = bc_ctx_p->current_p;
if (current_p + length > bc_ctx_p->block_end_p)
{
current_p = re_realloc_regexp_bytecode_block (bc_ctx_p);
}
memcpy (current_p, bytecode_p, length);
bc_ctx_p->current_p += length;
} /* re_bytecode_list_append */
/**
* Insert a new bytecode to the bytecode container
*/
void
re_bytecode_list_insert (re_bytecode_ctx_t *bc_ctx_p, /**< RegExp bytecode context */
size_t offset, /**< distance from the start of the container */
uint8_t *bytecode_p, /**< input bytecode */
size_t length) /**< length of input */
{
JERRY_ASSERT (length <= REGEXP_BYTECODE_BLOCK_SIZE);
uint8_t *current_p = bc_ctx_p->current_p;
if (current_p + length > bc_ctx_p->block_end_p)
{
re_realloc_regexp_bytecode_block (bc_ctx_p);
}
uint8_t *src_p = bc_ctx_p->block_start_p + offset;
if ((re_get_bytecode_length (bc_ctx_p) - offset) > 0)
{
uint8_t *dest_p = src_p + length;
uint8_t *tmp_block_start_p;
tmp_block_start_p = (uint8_t *) jmem_heap_alloc_block (re_get_bytecode_length (bc_ctx_p) - offset);
memcpy (tmp_block_start_p, src_p, (size_t) (re_get_bytecode_length (bc_ctx_p) - offset));
memcpy (dest_p, tmp_block_start_p, (size_t) (re_get_bytecode_length (bc_ctx_p) - offset));
jmem_heap_free_block (tmp_block_start_p, re_get_bytecode_length (bc_ctx_p) - offset);
}
memcpy (src_p, bytecode_p, length);
bc_ctx_p->current_p += length;
} /* re_bytecode_list_insert */
/**
* Get a character from the RegExp bytecode and increase the bytecode position
*
* @return ecma character
*/
ecma_char_t __attr_always_inline___
re_get_char (uint8_t **bc_p) /**< pointer to bytecode start */
{
ecma_char_t chr = *((ecma_char_t *) *bc_p);
(*bc_p) += sizeof (ecma_char_t);
return chr;
} /* re_get_char */
/**
* Get a RegExp opcode and increase the bytecode position
*
* @return current RegExp opcode
*/
re_opcode_t __attr_always_inline___
re_get_opcode (uint8_t **bc_p) /**< pointer to bytecode start */
{
uint8_t bytecode = **bc_p;
(*bc_p) += sizeof (uint8_t);
return (re_opcode_t) bytecode;
} /* re_get_opcode */
/**
* Get a parameter of a RegExp opcode and increase the bytecode position
*
* @return opcode parameter
*/
uint32_t __attr_always_inline___
re_get_value (uint8_t **bc_p) /**< pointer to bytecode start */
{
uint32_t value = *((uint32_t *) *bc_p);
(*bc_p) += sizeof (uint32_t);
return value;
} /* re_get_value */
/**
* Get length of bytecode
*
* @return bytecode length (unsigned integer)
*/
uint32_t __attr_pure___ __attr_always_inline___
re_get_bytecode_length (re_bytecode_ctx_t *bc_ctx_p) /**< RegExp bytecode context */
{
return ((uint32_t) (bc_ctx_p->current_p - bc_ctx_p->block_start_p));
} /* re_get_bytecode_length */
/**
* Append a RegExp opcode
*/
void
re_append_opcode (re_bytecode_ctx_t *bc_ctx_p, /**< RegExp bytecode context */
re_opcode_t opcode) /**< input opcode */
{
re_bytecode_list_append (bc_ctx_p, (uint8_t *) &opcode, sizeof (uint8_t));
} /* re_append_opcode */
/**
* Append a parameter of a RegExp opcode
*/
void
re_append_u32 (re_bytecode_ctx_t *bc_ctx_p, /**< RegExp bytecode context */
uint32_t value) /**< input value */
{
re_bytecode_list_append (bc_ctx_p, (uint8_t *) &value, sizeof (uint32_t));
} /* re_append_u32 */
/**
* Append a character to the RegExp bytecode
*/
void
re_append_char (re_bytecode_ctx_t *bc_ctx_p, /**< RegExp bytecode context */
ecma_char_t input_char) /**< input char */
{
re_bytecode_list_append (bc_ctx_p, (uint8_t *) &input_char, sizeof (ecma_char_t));
} /* re_append_char */
/**
* Append a jump offset parameter of a RegExp opcode
*/
void
re_append_jump_offset (re_bytecode_ctx_t *bc_ctx_p, /**< RegExp bytecode context */
uint32_t value) /**< input value */
{
value += (uint32_t) (sizeof (uint32_t));
re_append_u32 (bc_ctx_p, value);
} /* re_append_jump_offset */
/**
* Insert a RegExp opcode
*/
void
re_insert_opcode (re_bytecode_ctx_t *bc_ctx_p, /**< RegExp bytecode context */
uint32_t offset, /**< distance from the start of the container */
re_opcode_t opcode) /**< input opcode */
{
re_bytecode_list_insert (bc_ctx_p, offset, (uint8_t *) &opcode, sizeof (uint8_t));
} /* re_insert_opcode */
/**
* Insert a parameter of a RegExp opcode
*/
void
re_insert_u32 (re_bytecode_ctx_t *bc_ctx_p, /**< RegExp bytecode context */
uint32_t offset, /**< distance from the start of the container */
uint32_t value) /**< input value */
{
re_bytecode_list_insert (bc_ctx_p, offset, (uint8_t *) &value, sizeof (uint32_t));
} /* re_insert_u32 */
#ifdef REGEXP_DUMP_BYTE_CODE
/**
* RegExp bytecode dumper
*/
void
re_dump_bytecode (re_bytecode_ctx_t *bc_ctx_p) /**< RegExp bytecode context */
{
re_compiled_code_t *compiled_code_p = (re_compiled_code_t *) bc_ctx_p->block_start_p;
JERRY_DEBUG_MSG ("%d ", compiled_code_p->header.status_flags);
JERRY_DEBUG_MSG ("%d ", compiled_code_p->num_of_captures);
JERRY_DEBUG_MSG ("%d | ", compiled_code_p->num_of_non_captures);
uint8_t *bytecode_p = (uint8_t *) (compiled_code_p + 1);
re_opcode_t op;
while ((op = re_get_opcode (&bytecode_p)))
{
switch (op)
{
case RE_OP_MATCH:
{
JERRY_DEBUG_MSG ("MATCH, ");
break;
}
case RE_OP_CHAR:
{
JERRY_DEBUG_MSG ("CHAR ");
JERRY_DEBUG_MSG ("%c, ", (char) re_get_char (&bytecode_p));
break;
}
case RE_OP_CAPTURE_NON_GREEDY_ZERO_GROUP_START:
{
JERRY_DEBUG_MSG ("N");
/* FALLTHRU */
}
case RE_OP_CAPTURE_GREEDY_ZERO_GROUP_START:
{
JERRY_DEBUG_MSG ("GZ_START ");
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_CAPTURE_GROUP_START:
{
JERRY_DEBUG_MSG ("START ");
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_CAPTURE_NON_GREEDY_GROUP_END:
{
JERRY_DEBUG_MSG ("N");
/* FALLTHRU */
}
case RE_OP_CAPTURE_GREEDY_GROUP_END:
{
JERRY_DEBUG_MSG ("G_END ");
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_NON_CAPTURE_NON_GREEDY_ZERO_GROUP_START:
{
JERRY_DEBUG_MSG ("N");
/* FALLTHRU */
}
case RE_OP_NON_CAPTURE_GREEDY_ZERO_GROUP_START:
{
JERRY_DEBUG_MSG ("GZ_NC_START ");
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_NON_CAPTURE_GROUP_START:
{
JERRY_DEBUG_MSG ("NC_START ");
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_NON_CAPTURE_NON_GREEDY_GROUP_END:
{
JERRY_DEBUG_MSG ("N");
/* FALLTHRU */
}
case RE_OP_NON_CAPTURE_GREEDY_GROUP_END:
{
JERRY_DEBUG_MSG ("G_NC_END ");
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_SAVE_AT_START:
{
JERRY_DEBUG_MSG ("RE_START ");
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_SAVE_AND_MATCH:
{
JERRY_DEBUG_MSG ("RE_END, ");
break;
}
case RE_OP_GREEDY_ITERATOR:
{
JERRY_DEBUG_MSG ("GREEDY_ITERATOR ");
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_NON_GREEDY_ITERATOR:
{
JERRY_DEBUG_MSG ("NON_GREEDY_ITERATOR ");
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_PERIOD:
{
JERRY_DEBUG_MSG ("PERIOD ");
break;
}
case RE_OP_ALTERNATIVE:
{
JERRY_DEBUG_MSG ("ALTERNATIVE ");
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_ASSERT_START:
{
JERRY_DEBUG_MSG ("ASSERT_START ");
break;
}
case RE_OP_ASSERT_END:
{
JERRY_DEBUG_MSG ("ASSERT_END ");
break;
}
case RE_OP_ASSERT_WORD_BOUNDARY:
{
JERRY_DEBUG_MSG ("ASSERT_WORD_BOUNDARY ");
break;
}
case RE_OP_ASSERT_NOT_WORD_BOUNDARY:
{
JERRY_DEBUG_MSG ("ASSERT_NOT_WORD_BOUNDARY ");
break;
}
case RE_OP_LOOKAHEAD_POS:
{
JERRY_DEBUG_MSG ("LOOKAHEAD_POS ");
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_LOOKAHEAD_NEG:
{
JERRY_DEBUG_MSG ("LOOKAHEAD_NEG ");
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_BACKREFERENCE:
{
JERRY_DEBUG_MSG ("BACKREFERENCE ");
JERRY_DEBUG_MSG ("%d, ", re_get_value (&bytecode_p));
break;
}
case RE_OP_INV_CHAR_CLASS:
{
JERRY_DEBUG_MSG ("INV_");
/* FALLTHRU */
}
case RE_OP_CHAR_CLASS:
{
JERRY_DEBUG_MSG ("CHAR_CLASS ");
uint32_t num_of_class = re_get_value (&bytecode_p);
JERRY_DEBUG_MSG ("%d", num_of_class);
while (num_of_class)
{
JERRY_DEBUG_MSG (" %d", re_get_char (&bytecode_p));
JERRY_DEBUG_MSG ("-%d", re_get_char (&bytecode_p));
num_of_class--;
}
JERRY_DEBUG_MSG (", ");
break;
}
default:
{
JERRY_DEBUG_MSG ("UNKNOWN(%d), ", (uint32_t) op);
break;
}
}
}
JERRY_DEBUG_MSG ("EOF\n");
} /* re_dump_bytecode */
#endif /* REGEXP_DUMP_BYTE_CODE */
/**
* @}
* @}
* @}
*/
#endif /* !CONFIG_DISABLE_REGEXP_BUILTIN */

View file

@ -0,0 +1,129 @@
/* Copyright 2016 Samsung Electronics Co., Ltd.
* Copyright 2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RE_BYTECODE_H
#define RE_BYTECODE_H
#ifndef CONFIG_DISABLE_REGEXP_BUILTIN
#include "ecma-globals.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup regexparser Regular expression
* @{
*
* \addtogroup regexparser_bytecode Bytecode
* @{
*/
/**
* Size of the RegExp bytecode cache
*/
#define RE_CACHE_SIZE 8u
/**
* RegExp flags mask (first 10 bits are for reference count and the rest for the actual RegExp flags)
*/
#define RE_FLAGS_MASK 0x3F
/**
* RegExp opcodes
*/
typedef enum
{
RE_OP_EOF,
/* Group opcode order is important, because RE_IS_CAPTURE_GROUP is based on it.
* Change it carefully. Capture opcodes should be at first.
*/
RE_OP_CAPTURE_GROUP_START, /**< group start */
RE_OP_CAPTURE_GREEDY_ZERO_GROUP_START, /**< greedy zero group start */
RE_OP_CAPTURE_NON_GREEDY_ZERO_GROUP_START, /**< non-greedy zero group start */
RE_OP_CAPTURE_GREEDY_GROUP_END, /**< greedy group end */
RE_OP_CAPTURE_NON_GREEDY_GROUP_END, /**< non-greedy group end */
RE_OP_NON_CAPTURE_GROUP_START, /**< non-capture group start */
RE_OP_NON_CAPTURE_GREEDY_ZERO_GROUP_START, /**< non-capture greedy zero group start */
RE_OP_NON_CAPTURE_NON_GREEDY_ZERO_GROUP_START, /**< non-capture non-greedy zero group start */
RE_OP_NON_CAPTURE_GREEDY_GROUP_END, /**< non-capture greedy group end */
RE_OP_NON_CAPTURE_NON_GREEDY_GROUP_END, /**< non-capture non-greedy group end */
RE_OP_MATCH, /**< match */
RE_OP_CHAR, /**< any character */
RE_OP_SAVE_AT_START, /**< save at start */
RE_OP_SAVE_AND_MATCH, /**< save and match */
RE_OP_PERIOD, /**< "." */
RE_OP_ALTERNATIVE, /**< "|" */
RE_OP_GREEDY_ITERATOR, /**< greedy iterator */
RE_OP_NON_GREEDY_ITERATOR, /**< non-greedy iterator */
RE_OP_ASSERT_START, /**< "^" */
RE_OP_ASSERT_END, /**< "$" */
RE_OP_ASSERT_WORD_BOUNDARY, /**< "\b" */
RE_OP_ASSERT_NOT_WORD_BOUNDARY, /**< "\B" */
RE_OP_LOOKAHEAD_POS, /**< lookahead pos */
RE_OP_LOOKAHEAD_NEG, /**< lookahead neg */
RE_OP_BACKREFERENCE, /**< "\[0..9]" */
RE_OP_CHAR_CLASS, /**< "[ ]" */
RE_OP_INV_CHAR_CLASS /**< "[^ ]" */
} re_opcode_t;
/**
* Compiled byte code data.
*/
typedef struct
{
ecma_compiled_code_t header; /**< compiled code header */
jmem_cpointer_t pattern_cp; /**< original RegExp pattern */
uint32_t num_of_captures; /**< number of capturing brackets */
uint32_t num_of_non_captures; /**< number of non capturing brackets */
} re_compiled_code_t;
/**
* Context of RegExp bytecode container
*/
typedef struct
{
uint8_t *block_start_p; /**< start of bytecode block */
uint8_t *block_end_p; /**< end of bytecode block */
uint8_t *current_p; /**< current position in bytecode */
} re_bytecode_ctx_t;
re_opcode_t re_get_opcode (uint8_t **);
ecma_char_t re_get_char (uint8_t **);
uint32_t re_get_value (uint8_t **);
uint32_t re_get_bytecode_length (re_bytecode_ctx_t *);
void re_append_opcode (re_bytecode_ctx_t *, re_opcode_t);
void re_append_u32 (re_bytecode_ctx_t *, uint32_t);
void re_append_char (re_bytecode_ctx_t *, ecma_char_t);
void re_append_jump_offset (re_bytecode_ctx_t *, uint32_t);
void re_insert_opcode (re_bytecode_ctx_t *, uint32_t, re_opcode_t);
void re_insert_u32 (re_bytecode_ctx_t *, uint32_t, uint32_t);
void re_bytecode_list_insert (re_bytecode_ctx_t *, size_t, uint8_t *, size_t);
#ifdef REGEXP_DUMP_BYTE_CODE
void re_dump_bytecode (re_bytecode_ctx_t *bc_ctx);
#endif /* REGEXP_DUMP_BYTE_CODE */
/**
* @}
* @}
* @}
*/
#endif /* !CONFIG_DISABLE_REGEXP_BUILTIN */
#endif /* !RE_BYTECODE_H */

View file

@ -0,0 +1,650 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-exceptions.h"
#include "ecma-helpers.h"
#include "ecma-regexp-object.h"
#include "ecma-try-catch-macro.h"
#include "jcontext.h"
#include "jrt-libc-includes.h"
#include "jmem-heap.h"
#include "re-bytecode.h"
#include "re-compiler.h"
#include "re-parser.h"
#ifndef CONFIG_DISABLE_REGEXP_BUILTIN
/** \addtogroup parser Parser
* @{
*
* \addtogroup regexparser Regular expression
* @{
*
* \addtogroup regexparser_compiler Compiler
* @{
*/
/**
* Callback function of character class generation
*/
static void
re_append_char_class (void *re_ctx_p, /**< RegExp compiler context */
ecma_char_t start, /**< character class range from */
ecma_char_t end) /**< character class range to */
{
re_compiler_ctx_t *ctx_p = (re_compiler_ctx_t *) re_ctx_p;
re_append_char (ctx_p->bytecode_ctx_p, start);
re_append_char (ctx_p->bytecode_ctx_p, end);
ctx_p->parser_ctx_p->num_of_classes++;
} /* re_append_char_class */
/**
* Insert simple atom iterator
*/
static void
re_insert_simple_iterator (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context */
uint32_t new_atom_start_offset) /**< atom start offset */
{
uint32_t atom_code_length;
uint32_t offset;
uint32_t qmin, qmax;
qmin = re_ctx_p->current_token.qmin;
qmax = re_ctx_p->current_token.qmax;
JERRY_ASSERT (qmin <= qmax);
/* TODO: optimize bytecode length. Store 0 rather than INF */
re_append_opcode (re_ctx_p->bytecode_ctx_p, RE_OP_MATCH); /* complete 'sub atom' */
uint32_t bytecode_length = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p);
atom_code_length = (uint32_t) (bytecode_length - new_atom_start_offset);
offset = new_atom_start_offset;
re_insert_u32 (re_ctx_p->bytecode_ctx_p, offset, atom_code_length);
re_insert_u32 (re_ctx_p->bytecode_ctx_p, offset, qmax);
re_insert_u32 (re_ctx_p->bytecode_ctx_p, offset, qmin);
if (re_ctx_p->current_token.greedy)
{
re_insert_opcode (re_ctx_p->bytecode_ctx_p, offset, RE_OP_GREEDY_ITERATOR);
}
else
{
re_insert_opcode (re_ctx_p->bytecode_ctx_p, offset, RE_OP_NON_GREEDY_ITERATOR);
}
} /* re_insert_simple_iterator */
/**
* Get the type of a group start
*
* @return RegExp opcode
*/
static re_opcode_t
re_get_start_opcode_type (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context */
bool is_capturable) /**< is capturable group */
{
if (is_capturable)
{
if (re_ctx_p->current_token.qmin == 0)
{
if (re_ctx_p->current_token.greedy)
{
return RE_OP_CAPTURE_GREEDY_ZERO_GROUP_START;
}
return RE_OP_CAPTURE_NON_GREEDY_ZERO_GROUP_START;
}
return RE_OP_CAPTURE_GROUP_START;
}
if (re_ctx_p->current_token.qmin == 0)
{
if (re_ctx_p->current_token.greedy)
{
return RE_OP_NON_CAPTURE_GREEDY_ZERO_GROUP_START;
}
return RE_OP_NON_CAPTURE_NON_GREEDY_ZERO_GROUP_START;
}
return RE_OP_NON_CAPTURE_GROUP_START;
} /* re_get_start_opcode_type */
/**
* Get the type of a group end
*
* @return RegExp opcode
*/
static re_opcode_t
re_get_end_opcode_type (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context */
bool is_capturable) /**< is capturable group */
{
if (is_capturable)
{
if (re_ctx_p->current_token.greedy)
{
return RE_OP_CAPTURE_GREEDY_GROUP_END;
}
return RE_OP_CAPTURE_NON_GREEDY_GROUP_END;
}
if (re_ctx_p->current_token.greedy)
{
return RE_OP_NON_CAPTURE_GREEDY_GROUP_END;
}
return RE_OP_NON_CAPTURE_NON_GREEDY_GROUP_END;
} /* re_get_end_opcode_type */
/**
* Enclose the given bytecode to a group
*/
static void
re_insert_into_group (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context */
uint32_t group_start_offset, /**< offset of group start */
uint32_t idx, /**< index of group */
bool is_capturable) /**< is capturable group */
{
uint32_t qmin, qmax;
re_opcode_t start_opcode = re_get_start_opcode_type (re_ctx_p, is_capturable);
re_opcode_t end_opcode = re_get_end_opcode_type (re_ctx_p, is_capturable);
uint32_t start_head_offset_len;
qmin = re_ctx_p->current_token.qmin;
qmax = re_ctx_p->current_token.qmax;
JERRY_ASSERT (qmin <= qmax);
start_head_offset_len = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p);
re_insert_u32 (re_ctx_p->bytecode_ctx_p, group_start_offset, idx);
re_insert_opcode (re_ctx_p->bytecode_ctx_p, group_start_offset, start_opcode);
start_head_offset_len = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p) - start_head_offset_len;
re_append_opcode (re_ctx_p->bytecode_ctx_p, end_opcode);
re_append_u32 (re_ctx_p->bytecode_ctx_p, idx);
re_append_u32 (re_ctx_p->bytecode_ctx_p, qmin);
re_append_u32 (re_ctx_p->bytecode_ctx_p, qmax);
group_start_offset += start_head_offset_len;
re_append_jump_offset (re_ctx_p->bytecode_ctx_p,
re_get_bytecode_length (re_ctx_p->bytecode_ctx_p) - group_start_offset);
if (start_opcode != RE_OP_CAPTURE_GROUP_START && start_opcode != RE_OP_NON_CAPTURE_GROUP_START)
{
re_insert_u32 (re_ctx_p->bytecode_ctx_p,
group_start_offset,
re_get_bytecode_length (re_ctx_p->bytecode_ctx_p) - group_start_offset);
}
} /* re_insert_into_group */
/**
* Enclose the given bytecode to a group and inster jump value
*/
static void
re_insert_into_group_with_jump (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context */
uint32_t group_start_offset, /**< offset of group start */
uint32_t idx, /**< index of group */
bool is_capturable) /**< is capturable group */
{
re_insert_u32 (re_ctx_p->bytecode_ctx_p,
group_start_offset,
re_get_bytecode_length (re_ctx_p->bytecode_ctx_p) - group_start_offset);
re_insert_into_group (re_ctx_p, group_start_offset, idx, is_capturable);
} /* re_insert_into_group_with_jump */
/**
* Parse alternatives
*
* @return empty ecma value - if alternative was successfully parsed
* error ecma value - otherwise
*
* Returned value must be freed with ecma_free_value
*/
static ecma_value_t
re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context */
bool expect_eof) /**< expect end of file */
{
uint32_t idx;
re_bytecode_ctx_t *bc_ctx_p = re_ctx_p->bytecode_ctx_p;
ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
uint32_t alterantive_offset = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p);
bool should_loop = true;
while (ecma_is_value_empty (ret_value) && should_loop)
{
ECMA_TRY_CATCH (empty,
re_parse_next_token (re_ctx_p->parser_ctx_p,
&(re_ctx_p->current_token)),
ret_value);
uint32_t new_atom_start_offset = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p);
switch (re_ctx_p->current_token.type)
{
case RE_TOK_START_CAPTURE_GROUP:
{
idx = re_ctx_p->num_of_captures++;
JERRY_TRACE_MSG ("Compile a capture group start (idx: %u)\n", (unsigned int) idx);
ret_value = re_parse_alternative (re_ctx_p, false);
if (ecma_is_value_empty (ret_value))
{
re_insert_into_group (re_ctx_p, new_atom_start_offset, idx, true);
}
break;
}
case RE_TOK_START_NON_CAPTURE_GROUP:
{
idx = re_ctx_p->num_of_non_captures++;
JERRY_TRACE_MSG ("Compile a non-capture group start (idx: %u)\n", (unsigned int) idx);
ret_value = re_parse_alternative (re_ctx_p, false);
if (ecma_is_value_empty (ret_value))
{
re_insert_into_group (re_ctx_p, new_atom_start_offset, idx, false);
}
break;
}
case RE_TOK_CHAR:
{
JERRY_TRACE_MSG ("Compile character token: %c, qmin: %u, qmax: %u\n",
(char) re_ctx_p->current_token.value, (unsigned int) re_ctx_p->current_token.qmin,
(unsigned int) re_ctx_p->current_token.qmax);
re_append_opcode (bc_ctx_p, RE_OP_CHAR);
re_append_char (bc_ctx_p, re_canonicalize ((ecma_char_t) re_ctx_p->current_token.value,
re_ctx_p->flags & RE_FLAG_IGNORE_CASE));
if ((re_ctx_p->current_token.qmin != 1) || (re_ctx_p->current_token.qmax != 1))
{
re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
}
break;
}
case RE_TOK_PERIOD:
{
JERRY_TRACE_MSG ("Compile a period\n");
re_append_opcode (bc_ctx_p, RE_OP_PERIOD);
if ((re_ctx_p->current_token.qmin != 1) || (re_ctx_p->current_token.qmax != 1))
{
re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
}
break;
}
case RE_TOK_ALTERNATIVE:
{
JERRY_TRACE_MSG ("Compile an alternative\n");
re_insert_u32 (bc_ctx_p, alterantive_offset, re_get_bytecode_length (bc_ctx_p) - alterantive_offset);
re_append_opcode (bc_ctx_p, RE_OP_ALTERNATIVE);
alterantive_offset = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p);
break;
}
case RE_TOK_ASSERT_START:
{
JERRY_TRACE_MSG ("Compile a start assertion\n");
re_append_opcode (bc_ctx_p, RE_OP_ASSERT_START);
break;
}
case RE_TOK_ASSERT_END:
{
JERRY_TRACE_MSG ("Compile an end assertion\n");
re_append_opcode (bc_ctx_p, RE_OP_ASSERT_END);
break;
}
case RE_TOK_ASSERT_WORD_BOUNDARY:
{
JERRY_TRACE_MSG ("Compile a word boundary assertion\n");
re_append_opcode (bc_ctx_p, RE_OP_ASSERT_WORD_BOUNDARY);
break;
}
case RE_TOK_ASSERT_NOT_WORD_BOUNDARY:
{
JERRY_TRACE_MSG ("Compile a not word boundary assertion\n");
re_append_opcode (bc_ctx_p, RE_OP_ASSERT_NOT_WORD_BOUNDARY);
break;
}
case RE_TOK_ASSERT_START_POS_LOOKAHEAD:
{
JERRY_TRACE_MSG ("Compile a positive lookahead assertion\n");
idx = re_ctx_p->num_of_non_captures++;
re_append_opcode (bc_ctx_p, RE_OP_LOOKAHEAD_POS);
ret_value = re_parse_alternative (re_ctx_p, false);
if (ecma_is_value_empty (ret_value))
{
re_append_opcode (bc_ctx_p, RE_OP_MATCH);
re_insert_into_group_with_jump (re_ctx_p, new_atom_start_offset, idx, false);
}
break;
}
case RE_TOK_ASSERT_START_NEG_LOOKAHEAD:
{
JERRY_TRACE_MSG ("Compile a negative lookahead assertion\n");
idx = re_ctx_p->num_of_non_captures++;
re_append_opcode (bc_ctx_p, RE_OP_LOOKAHEAD_NEG);
ret_value = re_parse_alternative (re_ctx_p, false);
if (ecma_is_value_empty (ret_value))
{
re_append_opcode (bc_ctx_p, RE_OP_MATCH);
re_insert_into_group_with_jump (re_ctx_p, new_atom_start_offset, idx, false);
}
break;
}
case RE_TOK_BACKREFERENCE:
{
uint32_t backref = (uint32_t) re_ctx_p->current_token.value;
idx = re_ctx_p->num_of_non_captures++;
if (backref > re_ctx_p->highest_backref)
{
re_ctx_p->highest_backref = backref;
}
JERRY_TRACE_MSG ("Compile a backreference: %u\n", (unsigned int) backref);
re_append_opcode (bc_ctx_p, RE_OP_BACKREFERENCE);
re_append_u32 (bc_ctx_p, backref);
re_insert_into_group_with_jump (re_ctx_p, new_atom_start_offset, idx, false);
break;
}
case RE_TOK_DIGIT:
case RE_TOK_NOT_DIGIT:
case RE_TOK_WHITE:
case RE_TOK_NOT_WHITE:
case RE_TOK_WORD_CHAR:
case RE_TOK_NOT_WORD_CHAR:
case RE_TOK_START_CHAR_CLASS:
case RE_TOK_START_INV_CHAR_CLASS:
{
JERRY_TRACE_MSG ("Compile a character class\n");
re_append_opcode (bc_ctx_p,
re_ctx_p->current_token.type == RE_TOK_START_INV_CHAR_CLASS
? RE_OP_INV_CHAR_CLASS
: RE_OP_CHAR_CLASS);
uint32_t offset = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p);
ECMA_TRY_CATCH (empty,
re_parse_char_class (re_ctx_p->parser_ctx_p,
re_append_char_class,
re_ctx_p,
&(re_ctx_p->current_token)),
ret_value);
re_insert_u32 (bc_ctx_p, offset, re_ctx_p->parser_ctx_p->num_of_classes);
if ((re_ctx_p->current_token.qmin != 1) || (re_ctx_p->current_token.qmax != 1))
{
re_insert_simple_iterator (re_ctx_p, new_atom_start_offset);
}
ECMA_FINALIZE (empty);
break;
}
case RE_TOK_END_GROUP:
{
JERRY_TRACE_MSG ("Compile a group end\n");
if (expect_eof)
{
ret_value = ecma_raise_syntax_error (ECMA_ERR_MSG ("Unexpected end of paren."));
}
else
{
re_insert_u32 (bc_ctx_p, alterantive_offset, re_get_bytecode_length (bc_ctx_p) - alterantive_offset);
should_loop = false;
}
break;
}
case RE_TOK_EOF:
{
if (!expect_eof)
{
ret_value = ecma_raise_syntax_error (ECMA_ERR_MSG ("Unexpected end of pattern."));
}
else
{
re_insert_u32 (bc_ctx_p, alterantive_offset, re_get_bytecode_length (bc_ctx_p) - alterantive_offset);
should_loop = false;
}
break;
}
default:
{
ret_value = ecma_raise_syntax_error (ECMA_ERR_MSG ("Unexpected RegExp token."));
break;
}
}
ECMA_FINALIZE (empty);
}
return ret_value;
} /* re_parse_alternative */
/**
* Search for the given pattern in the RegExp cache
*
* @return index of bytecode in cache - if found
* RE_CACHE_SIZE - otherwise
*/
static uint8_t
re_find_bytecode_in_cache (ecma_string_t *pattern_str_p, /**< pattern string */
uint16_t flags) /**< flags */
{
uint8_t free_idx = RE_CACHE_SIZE;
for (uint8_t idx = 0u; idx < RE_CACHE_SIZE; idx++)
{
const re_compiled_code_t *cached_bytecode_p = JERRY_CONTEXT (re_cache)[idx];
if (cached_bytecode_p != NULL)
{
ecma_string_t *cached_pattern_str_p;
cached_pattern_str_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t, cached_bytecode_p->pattern_cp);
if ((cached_bytecode_p->header.status_flags & RE_FLAGS_MASK) == flags
&& ecma_compare_ecma_strings (cached_pattern_str_p, pattern_str_p))
{
JERRY_TRACE_MSG ("RegExp is found in cache\n");
return idx;
}
}
else
{
/* mark as free, so it can be overridden if the cache is full */
free_idx = idx;
}
}
JERRY_TRACE_MSG ("RegExp is NOT found in cache\n");
return free_idx;
} /* re_find_bytecode_in_cache */
/**
* Run gerbage collection in RegExp cache
*/
void
re_cache_gc_run ()
{
for (uint32_t i = 0u; i < RE_CACHE_SIZE; i++)
{
const re_compiled_code_t *cached_bytecode_p = JERRY_CONTEXT (re_cache)[i];
if (cached_bytecode_p != NULL
&& cached_bytecode_p->header.refs == 1)
{
/* Only the cache has reference for the bytecode */
ecma_bytecode_deref ((ecma_compiled_code_t *) cached_bytecode_p);
JERRY_CONTEXT (re_cache)[i] = NULL;
}
}
} /* re_cache_gc_run */
/**
* Compilation of RegExp bytecode
*
* @return empty ecma value - if bytecode was compiled successfully
* error ecma value - otherwise
*
* Returned value must be freed with ecma_free_value
*/
ecma_value_t
re_compile_bytecode (const re_compiled_code_t **out_bytecode_p, /**< [out] pointer to bytecode */
ecma_string_t *pattern_str_p, /**< pattern */
uint16_t flags) /**< flags */
{
ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
uint8_t cache_idx = re_find_bytecode_in_cache (pattern_str_p, flags);
if (cache_idx < RE_CACHE_SIZE)
{
*out_bytecode_p = JERRY_CONTEXT (re_cache)[cache_idx];
if (*out_bytecode_p != NULL)
{
ecma_bytecode_ref ((ecma_compiled_code_t *) *out_bytecode_p);
return ret_value;
}
}
/* not in the RegExp cache, so compile it */
re_compiler_ctx_t re_ctx;
re_ctx.flags = flags;
re_ctx.highest_backref = 0;
re_ctx.num_of_non_captures = 0;
re_bytecode_ctx_t bc_ctx;
bc_ctx.block_start_p = NULL;
bc_ctx.block_end_p = NULL;
bc_ctx.current_p = NULL;
re_ctx.bytecode_ctx_p = &bc_ctx;
ECMA_STRING_TO_UTF8_STRING (pattern_str_p, pattern_start_p, pattern_start_size);
re_parser_ctx_t parser_ctx;
parser_ctx.input_start_p = pattern_start_p;
parser_ctx.input_curr_p = (lit_utf8_byte_t *) pattern_start_p;
parser_ctx.input_end_p = pattern_start_p + pattern_start_size;
parser_ctx.num_of_groups = -1;
re_ctx.parser_ctx_p = &parser_ctx;
/* 1. Parse RegExp pattern */
re_ctx.num_of_captures = 1;
re_append_opcode (&bc_ctx, RE_OP_SAVE_AT_START);
ECMA_TRY_CATCH (empty, re_parse_alternative (&re_ctx, true), ret_value);
/* 2. Check for invalid backreference */
if (re_ctx.highest_backref >= re_ctx.num_of_captures)
{
ret_value = ecma_raise_syntax_error ("Invalid backreference.\n");
}
else
{
re_append_opcode (&bc_ctx, RE_OP_SAVE_AND_MATCH);
re_append_opcode (&bc_ctx, RE_OP_EOF);
/* 3. Insert extra informations for bytecode header */
re_compiled_code_t re_compiled_code;
re_compiled_code.header.refs = 1;
re_compiled_code.header.status_flags = re_ctx.flags;
ecma_ref_ecma_string (pattern_str_p);
ECMA_SET_NON_NULL_POINTER (re_compiled_code.pattern_cp, pattern_str_p);
re_compiled_code.num_of_captures = re_ctx.num_of_captures * 2;
re_compiled_code.num_of_non_captures = re_ctx.num_of_non_captures;
re_bytecode_list_insert (&bc_ctx,
0,
(uint8_t *) &re_compiled_code,
sizeof (re_compiled_code_t));
}
ECMA_FINALIZE (empty);
ECMA_FINALIZE_UTF8_STRING (pattern_start_p, pattern_start_size);
size_t byte_code_size = (size_t) (bc_ctx.block_end_p - bc_ctx.block_start_p);
if (!ecma_is_value_empty (ret_value))
{
/* Compilation failed, free bytecode. */
JERRY_TRACE_MSG ("RegExp compilation failed!\n");
jmem_heap_free_block (bc_ctx.block_start_p, byte_code_size);
*out_bytecode_p = NULL;
}
else
{
#ifdef REGEXP_DUMP_BYTE_CODE
if (JERRY_CONTEXT (jerry_init_flags) & JERRY_INIT_SHOW_REGEXP_OPCODES)
{
re_dump_bytecode (&bc_ctx);
}
#endif /* REGEXP_DUMP_BYTE_CODE */
/* The RegExp bytecode contains at least a RE_OP_SAVE_AT_START opdoce, so it cannot be NULL. */
JERRY_ASSERT (bc_ctx.block_start_p != NULL);
*out_bytecode_p = (re_compiled_code_t *) bc_ctx.block_start_p;
((re_compiled_code_t *) bc_ctx.block_start_p)->header.size = (uint16_t) (byte_code_size >> JMEM_ALIGNMENT_LOG);
if (cache_idx == RE_CACHE_SIZE)
{
if (JERRY_CONTEXT (re_cache_idx) == RE_CACHE_SIZE)
{
JERRY_CONTEXT (re_cache_idx) = 0;
}
JERRY_TRACE_MSG ("RegExp cache is full! Remove the element on idx: %d\n", JERRY_CONTEXT (re_cache_idx));
cache_idx = JERRY_CONTEXT (re_cache_idx)++;
/* The garbage collector might run during the byte code
* allocations above and it may free this entry. */
if (JERRY_CONTEXT (re_cache)[cache_idx] != NULL)
{
ecma_bytecode_deref ((ecma_compiled_code_t *) JERRY_CONTEXT (re_cache)[cache_idx]);
}
}
JERRY_TRACE_MSG ("Insert bytecode into RegExp cache (idx: %d).\n", cache_idx);
ecma_bytecode_ref ((ecma_compiled_code_t *) *out_bytecode_p);
JERRY_CONTEXT (re_cache)[cache_idx] = *out_bytecode_p;
}
return ret_value;
} /* re_compile_bytecode */
/**
* @}
* @}
* @}
*/
#endif /* !CONFIG_DISABLE_REGEXP_BUILTIN */

View file

@ -0,0 +1,62 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RE_COMPILER_H
#define RE_COMPILER_H
#ifndef CONFIG_DISABLE_REGEXP_BUILTIN
#include "ecma-globals.h"
#include "re-bytecode.h"
#include "re-parser.h"
/** \addtogroup parser Parser
* @{
*
* \addtogroup regexparser Regular expression
* @{
*
* \addtogroup regexparser_compiler Compiler
* @{
*/
/**
* Context of RegExp compiler
*/
typedef struct
{
uint16_t flags; /**< RegExp flags */
uint32_t num_of_captures; /**< number of capture groups */
uint32_t num_of_non_captures; /**< number of non-capture groups */
uint32_t highest_backref; /**< highest backreference */
re_bytecode_ctx_t *bytecode_ctx_p; /**< pointer of RegExp bytecode context */
re_token_t current_token; /**< current token */
re_parser_ctx_t *parser_ctx_p; /**< pointer of RegExp parser context */
} re_compiler_ctx_t;
ecma_value_t
re_compile_bytecode (const re_compiled_code_t **, ecma_string_t *, uint16_t);
void re_cache_gc_run ();
/**
* @}
* @}
* @}
*/
#endif /* !CONFIG_DISABLE_REGEXP_BUILTIN */
#endif /* !RE_COMPILER_H */

View file

@ -0,0 +1,920 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-exceptions.h"
#include "ecma-globals.h"
#include "ecma-try-catch-macro.h"
#include "jrt-libc-includes.h"
#include "lit-char-helpers.h"
#include "re-compiler.h"
#include "re-parser.h"
#ifndef CONFIG_DISABLE_REGEXP_BUILTIN
/** \addtogroup parser Parser
* @{
*
* \addtogroup regexparser Regular expression
* @{
*
* \addtogroup regexparser_parser Parser
* @{
*/
/**
* Lookup a character in the input string.
*
* @return true, if lookup number of characters ahead are hex digits
* false, otherwise
*/
static bool
re_hex_lookup (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context */
uint32_t lookup) /**< size of lookup */
{
bool is_digit = true;
const lit_utf8_byte_t *curr_p = parser_ctx_p->input_curr_p;
for (uint32_t i = 0; is_digit && i < lookup; i++)
{
if (curr_p < parser_ctx_p->input_end_p)
{
is_digit = lit_char_is_hex_digit (*curr_p++);
}
else
{
return false;
}
}
return is_digit;
} /* re_hex_lookup */
/**
* Consume non greedy (question mark) character if present.
*
* @return true, if non-greedy character found
* false, otherwise
*/
static inline bool __attr_always_inline___
re_parse_non_greedy_char (re_parser_ctx_t *parser_ctx_p) /**< RegExp parser context */
{
if (parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p
&& *parser_ctx_p->input_curr_p == LIT_CHAR_QUESTION)
{
parser_ctx_p->input_curr_p++;
return true;
}
return false;
} /* re_parse_non_greedy_char */
/**
* Parse a max 3 digit long octal number from input string iterator.
*
* @return uint32_t - parsed octal number
*/
static uint32_t
re_parse_octal (re_parser_ctx_t *parser_ctx_p) /**< RegExp parser context */
{
uint32_t number = 0;
for (int index = 0;
index < 3
&& parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p
&& lit_char_is_octal_digit (*parser_ctx_p->input_curr_p);
index++)
{
number = number * 8 + lit_char_hex_to_int (*parser_ctx_p->input_curr_p++);
}
return number;
} /* re_parse_octal */
/**
* Parse RegExp iterators
*
* @return empty ecma value - if parsed successfully
* error ecma value - otherwise
*
* Returned value must be freed with ecma_free_value
*/
static ecma_value_t
re_parse_iterator (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context */
re_token_t *re_token_p) /**< [out] output token */
{
ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
re_token_p->qmin = 1;
re_token_p->qmax = 1;
re_token_p->greedy = true;
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
return ret_value;
}
ecma_char_t ch = *parser_ctx_p->input_curr_p;
switch (ch)
{
case LIT_CHAR_QUESTION:
{
parser_ctx_p->input_curr_p++;
re_token_p->qmin = 0;
re_token_p->qmax = 1;
re_token_p->greedy = !re_parse_non_greedy_char (parser_ctx_p);
break;
}
case LIT_CHAR_ASTERISK:
{
parser_ctx_p->input_curr_p++;
re_token_p->qmin = 0;
re_token_p->qmax = RE_ITERATOR_INFINITE;
re_token_p->greedy = !re_parse_non_greedy_char (parser_ctx_p);
break;
}
case LIT_CHAR_PLUS:
{
parser_ctx_p->input_curr_p++;
re_token_p->qmin = 1;
re_token_p->qmax = RE_ITERATOR_INFINITE;
re_token_p->greedy = !re_parse_non_greedy_char (parser_ctx_p);
break;
}
case LIT_CHAR_LEFT_BRACE:
{
parser_ctx_p->input_curr_p++;
uint32_t qmin = 0;
uint32_t qmax = RE_ITERATOR_INFINITE;
uint32_t digits = 0;
while (true)
{
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("invalid quantifier"));
}
ch = *parser_ctx_p->input_curr_p++;
if (lit_char_is_decimal_digit (ch))
{
if (digits >= ECMA_NUMBER_MAX_DIGITS)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("RegExp quantifier error: too many digits."));
}
digits++;
qmin = qmin * 10 + lit_char_hex_to_int (ch);
}
else if (ch == LIT_CHAR_COMMA)
{
if (qmax != RE_ITERATOR_INFINITE)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("RegExp quantifier error: double comma."));
}
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("invalid quantifier"));
}
if (*parser_ctx_p->input_curr_p == LIT_CHAR_RIGHT_BRACE)
{
if (digits == 0)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("RegExp quantifier error: missing digits."));
}
parser_ctx_p->input_curr_p++;
re_token_p->qmin = qmin;
re_token_p->qmax = RE_ITERATOR_INFINITE;
break;
}
qmax = qmin;
qmin = 0;
digits = 0;
}
else if (ch == LIT_CHAR_RIGHT_BRACE)
{
if (digits == 0)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("RegExp quantifier error: missing digits."));
}
if (qmax != RE_ITERATOR_INFINITE)
{
re_token_p->qmin = qmax;
re_token_p->qmax = qmin;
}
else
{
re_token_p->qmin = qmin;
re_token_p->qmax = qmin;
}
break;
}
else
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("RegExp quantifier error: unknown char."));
}
}
re_token_p->greedy = !re_parse_non_greedy_char (parser_ctx_p);
break;
}
default:
{
break;
}
}
JERRY_ASSERT (ecma_is_value_empty (ret_value));
if (re_token_p->qmin > re_token_p->qmax)
{
ret_value = ecma_raise_syntax_error (ECMA_ERR_MSG ("RegExp quantifier error: qmin > qmax."));
}
return ret_value;
} /* re_parse_iterator */
/**
* Count the number of groups in pattern
*/
static void
re_count_num_of_groups (re_parser_ctx_t *parser_ctx_p) /**< RegExp parser context */
{
int char_class_in = 0;
parser_ctx_p->num_of_groups = 0;
const lit_utf8_byte_t *curr_p = parser_ctx_p->input_start_p;
while (curr_p < parser_ctx_p->input_end_p)
{
switch (*curr_p++)
{
case LIT_CHAR_BACKSLASH:
{
lit_utf8_incr (&curr_p);
break;
}
case LIT_CHAR_LEFT_SQUARE:
{
char_class_in++;
break;
}
case LIT_CHAR_RIGHT_SQUARE:
{
if (char_class_in)
{
char_class_in--;
}
break;
}
case LIT_CHAR_LEFT_PAREN:
{
if (curr_p < parser_ctx_p->input_end_p
&& *curr_p != LIT_CHAR_QUESTION
&& !char_class_in)
{
parser_ctx_p->num_of_groups++;
}
break;
}
}
}
} /* re_count_num_of_groups */
/**
* Read the input pattern and parse the range of character class
*
* @return empty ecma value - if parsed successfully
* error ecma value - otherwise
*
* Returned value must be freed with ecma_free_value
*/
ecma_value_t
re_parse_char_class (re_parser_ctx_t *parser_ctx_p, /**< number of classes */
re_char_class_callback append_char_class, /**< callback function,
* which adds the char-ranges
* to the bytecode */
void *re_ctx_p, /**< regexp compiler context */
re_token_t *out_token_p) /**< [out] output token */
{
re_token_type_t token_type = ((re_compiler_ctx_t *) re_ctx_p)->current_token.type;
out_token_p->qmax = out_token_p->qmin = 1;
ecma_char_t start = LIT_CHAR_UNDEF;
bool is_range = false;
parser_ctx_p->num_of_classes = 0;
if (lit_utf8_peek_prev (parser_ctx_p->input_curr_p) != LIT_CHAR_LEFT_SQUARE)
{
lit_utf8_decr (&parser_ctx_p->input_curr_p);
lit_utf8_decr (&parser_ctx_p->input_curr_p);
}
do
{
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("invalid character class, end of string"));
}
ecma_char_t ch = lit_utf8_read_next (&parser_ctx_p->input_curr_p);
if (ch == LIT_CHAR_RIGHT_SQUARE)
{
if (start != LIT_CHAR_UNDEF)
{
append_char_class (re_ctx_p, start, start);
}
break;
}
else if (ch == LIT_CHAR_MINUS)
{
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("invalid character class, end of string after '-'"));
}
if (start != LIT_CHAR_UNDEF
&& !is_range
&& *parser_ctx_p->input_curr_p != LIT_CHAR_RIGHT_SQUARE)
{
is_range = true;
continue;
}
}
else if (ch == LIT_CHAR_BACKSLASH)
{
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("invalid character class, end of string after '\\'"));
}
ch = *parser_ctx_p->input_curr_p++;
if (ch == LIT_CHAR_LOWERCASE_B)
{
ch = LIT_CHAR_BS;
}
else if (ch == LIT_CHAR_LOWERCASE_F)
{
ch = LIT_CHAR_FF;
}
else if (ch == LIT_CHAR_LOWERCASE_N)
{
ch = LIT_CHAR_LF;
}
else if (ch == LIT_CHAR_LOWERCASE_T)
{
ch = LIT_CHAR_TAB;
}
else if (ch == LIT_CHAR_LOWERCASE_R)
{
ch = LIT_CHAR_CR;
}
else if (ch == LIT_CHAR_LOWERCASE_V)
{
ch = LIT_CHAR_VTAB;
}
else if (ch == LIT_CHAR_LOWERCASE_C)
{
if (parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p)
{
ch = *parser_ctx_p->input_curr_p;
if ((ch >= LIT_CHAR_ASCII_UPPERCASE_LETTERS_BEGIN && ch <= LIT_CHAR_ASCII_UPPERCASE_LETTERS_END)
|| (ch >= LIT_CHAR_ASCII_LOWERCASE_LETTERS_BEGIN && ch <= LIT_CHAR_ASCII_LOWERCASE_LETTERS_END)
|| (ch >= LIT_CHAR_0 && ch <= LIT_CHAR_9))
{
/* See ECMA-262 v5, 15.10.2.10 (Point 3) */
ch = (ch % 32);
parser_ctx_p->input_curr_p++;
}
else
{
ch = LIT_CHAR_LOWERCASE_C;
}
}
}
else if (ch == LIT_CHAR_LOWERCASE_X)
{
ecma_char_t code_unit;
if (!lit_read_code_unit_from_hex (parser_ctx_p->input_curr_p, 2, &code_unit))
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("invalid character class, end of string after '\\x'"));
}
parser_ctx_p->input_curr_p += 2;
append_char_class (re_ctx_p, code_unit, code_unit);
ch = LIT_CHAR_UNDEF;
}
else if (ch == LIT_CHAR_LOWERCASE_U)
{
ecma_char_t code_unit;
if (!lit_read_code_unit_from_hex (parser_ctx_p->input_curr_p, 4, &code_unit))
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("invalid character class, end of string after '\\u'"));
}
parser_ctx_p->input_curr_p += 4;
append_char_class (re_ctx_p, code_unit, code_unit);
ch = LIT_CHAR_UNDEF;
}
else if (ch == LIT_CHAR_LOWERCASE_D)
{
/* See ECMA-262 v5, 15.10.2.12 */
append_char_class (re_ctx_p, LIT_CHAR_ASCII_DIGITS_BEGIN, LIT_CHAR_ASCII_DIGITS_END);
ch = LIT_CHAR_UNDEF;
}
else if (ch == LIT_CHAR_UPPERCASE_D)
{
/* See ECMA-262 v5, 15.10.2.12 */
append_char_class (re_ctx_p, LIT_CHAR_NULL, LIT_CHAR_ASCII_DIGITS_BEGIN - 1);
append_char_class (re_ctx_p, LIT_CHAR_ASCII_DIGITS_END + 1, LIT_UTF16_CODE_UNIT_MAX);
ch = LIT_CHAR_UNDEF;
}
else if (ch == LIT_CHAR_LOWERCASE_S)
{
/* See ECMA-262 v5, 15.10.2.12 */
append_char_class (re_ctx_p, LIT_CHAR_TAB, LIT_CHAR_CR);
append_char_class (re_ctx_p, LIT_CHAR_SP, LIT_CHAR_SP);
append_char_class (re_ctx_p, LIT_CHAR_NBSP, LIT_CHAR_NBSP);
append_char_class (re_ctx_p, 0x1680UL, 0x1680UL); /* Ogham Space Mark */
append_char_class (re_ctx_p, 0x180EUL, 0x180EUL); /* Mongolian Vowel Separator */
append_char_class (re_ctx_p, 0x2000UL, 0x200AUL); /* En Quad - Hair Space */
append_char_class (re_ctx_p, LIT_CHAR_LS, LIT_CHAR_PS);
append_char_class (re_ctx_p, 0x202FUL, 0x202FUL); /* Narrow No-Break Space */
append_char_class (re_ctx_p, 0x205FUL, 0x205FUL); /* Medium Mathematical Space */
append_char_class (re_ctx_p, 0x3000UL, 0x3000UL); /* Ideographic Space */
append_char_class (re_ctx_p, LIT_CHAR_BOM, LIT_CHAR_BOM);
ch = LIT_CHAR_UNDEF;
}
else if (ch == LIT_CHAR_UPPERCASE_S)
{
/* See ECMA-262 v5, 15.10.2.12 */
append_char_class (re_ctx_p, LIT_CHAR_NULL, LIT_CHAR_TAB - 1);
append_char_class (re_ctx_p, LIT_CHAR_CR + 1, LIT_CHAR_SP - 1);
append_char_class (re_ctx_p, LIT_CHAR_SP + 1, LIT_CHAR_NBSP - 1);
append_char_class (re_ctx_p, LIT_CHAR_NBSP + 1, 0x167FUL);
append_char_class (re_ctx_p, 0x1681UL, 0x180DUL);
append_char_class (re_ctx_p, 0x180FUL, 0x1FFFUL);
append_char_class (re_ctx_p, 0x200BUL, LIT_CHAR_LS - 1);
append_char_class (re_ctx_p, LIT_CHAR_PS + 1, 0x202EUL);
append_char_class (re_ctx_p, 0x2030UL, 0x205EUL);
append_char_class (re_ctx_p, 0x2060UL, 0x2FFFUL);
append_char_class (re_ctx_p, 0x3001UL, LIT_CHAR_BOM - 1);
append_char_class (re_ctx_p, LIT_CHAR_BOM + 1, LIT_UTF16_CODE_UNIT_MAX);
ch = LIT_CHAR_UNDEF;
}
else if (ch == LIT_CHAR_LOWERCASE_W)
{
/* See ECMA-262 v5, 15.10.2.12 */
append_char_class (re_ctx_p, LIT_CHAR_0, LIT_CHAR_9);
append_char_class (re_ctx_p, LIT_CHAR_UPPERCASE_A, LIT_CHAR_UPPERCASE_Z);
append_char_class (re_ctx_p, LIT_CHAR_UNDERSCORE, LIT_CHAR_UNDERSCORE);
append_char_class (re_ctx_p, LIT_CHAR_LOWERCASE_A, LIT_CHAR_LOWERCASE_Z);
ch = LIT_CHAR_UNDEF;
}
else if (ch == LIT_CHAR_UPPERCASE_W)
{
/* See ECMA-262 v5, 15.10.2.12 */
append_char_class (re_ctx_p, LIT_CHAR_NULL, LIT_CHAR_0 - 1);
append_char_class (re_ctx_p, LIT_CHAR_9 + 1, LIT_CHAR_UPPERCASE_A - 1);
append_char_class (re_ctx_p, LIT_CHAR_UPPERCASE_Z + 1, LIT_CHAR_UNDERSCORE - 1);
append_char_class (re_ctx_p, LIT_CHAR_UNDERSCORE + 1, LIT_CHAR_LOWERCASE_A - 1);
append_char_class (re_ctx_p, LIT_CHAR_LOWERCASE_Z + 1, LIT_UTF16_CODE_UNIT_MAX);
ch = LIT_CHAR_UNDEF;
}
else if (lit_char_is_octal_digit ((ecma_char_t) ch)
&& ch != LIT_CHAR_0)
{
parser_ctx_p->input_curr_p--;
ch = (ecma_char_t) re_parse_octal (parser_ctx_p);
}
} /* ch == LIT_CHAR_BACKSLASH */
if (ch == LIT_CHAR_UNDEF)
{
if (start != LIT_CHAR_UNDEF)
{
if (is_range)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("invalid character class, invalid range"));
}
else
{
append_char_class (re_ctx_p, start, start);
start = LIT_CHAR_UNDEF;
}
}
}
else
{
if (start != LIT_CHAR_UNDEF)
{
if (is_range)
{
if (start > ch)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("invalid character class, wrong order"));
}
else
{
append_char_class (re_ctx_p, start, ch);
start = LIT_CHAR_UNDEF;
is_range = false;
}
}
else
{
append_char_class (re_ctx_p, start, start);
start = ch;
}
}
else
{
start = ch;
}
}
}
while (token_type == RE_TOK_START_CHAR_CLASS || token_type == RE_TOK_START_INV_CHAR_CLASS);
return re_parse_iterator (parser_ctx_p, out_token_p);
} /* re_parse_char_class */
/**
* Read the input pattern and parse the next token for the RegExp compiler
*
* @return empty ecma value - if parsed successfully
* error ecma value - otherwise
*
* Returned value must be freed with ecma_free_value
*/
ecma_value_t
re_parse_next_token (re_parser_ctx_t *parser_ctx_p, /**< RegExp parser context */
re_token_t *out_token_p) /**< [out] output token */
{
ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
out_token_p->type = RE_TOK_EOF;
return ret_value;
}
ecma_char_t ch = lit_utf8_read_next (&parser_ctx_p->input_curr_p);
switch (ch)
{
case LIT_CHAR_VLINE:
{
out_token_p->type = RE_TOK_ALTERNATIVE;
break;
}
case LIT_CHAR_CIRCUMFLEX:
{
out_token_p->type = RE_TOK_ASSERT_START;
break;
}
case LIT_CHAR_DOLLAR_SIGN:
{
out_token_p->type = RE_TOK_ASSERT_END;
break;
}
case LIT_CHAR_DOT:
{
out_token_p->type = RE_TOK_PERIOD;
ret_value = re_parse_iterator (parser_ctx_p, out_token_p);
break;
}
case LIT_CHAR_BACKSLASH:
{
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("invalid regular experssion"));
}
out_token_p->type = RE_TOK_CHAR;
ch = lit_utf8_read_next (&parser_ctx_p->input_curr_p);
if (ch == LIT_CHAR_LOWERCASE_B)
{
out_token_p->type = RE_TOK_ASSERT_WORD_BOUNDARY;
}
else if (ch == LIT_CHAR_UPPERCASE_B)
{
out_token_p->type = RE_TOK_ASSERT_NOT_WORD_BOUNDARY;
}
else if (ch == LIT_CHAR_LOWERCASE_F)
{
out_token_p->value = LIT_CHAR_FF;
}
else if (ch == LIT_CHAR_LOWERCASE_N)
{
out_token_p->value = LIT_CHAR_LF;
}
else if (ch == LIT_CHAR_LOWERCASE_T)
{
out_token_p->value = LIT_CHAR_TAB;
}
else if (ch == LIT_CHAR_LOWERCASE_R)
{
out_token_p->value = LIT_CHAR_CR;
}
else if (ch == LIT_CHAR_LOWERCASE_V)
{
out_token_p->value = LIT_CHAR_VTAB;
}
else if (ch == LIT_CHAR_LOWERCASE_C)
{
if (parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p)
{
ch = *parser_ctx_p->input_curr_p;
if ((ch >= LIT_CHAR_ASCII_UPPERCASE_LETTERS_BEGIN && ch <= LIT_CHAR_ASCII_UPPERCASE_LETTERS_END)
|| (ch >= LIT_CHAR_ASCII_LOWERCASE_LETTERS_BEGIN && ch <= LIT_CHAR_ASCII_LOWERCASE_LETTERS_END))
{
out_token_p->value = (ch % 32);
parser_ctx_p->input_curr_p++;
}
else
{
out_token_p->value = LIT_CHAR_BACKSLASH;
parser_ctx_p->input_curr_p--;
}
}
else
{
out_token_p->value = LIT_CHAR_BACKSLASH;
parser_ctx_p->input_curr_p--;
}
}
else if (ch == LIT_CHAR_LOWERCASE_X
&& re_hex_lookup (parser_ctx_p, 2))
{
ecma_char_t code_unit;
if (!lit_read_code_unit_from_hex (parser_ctx_p->input_curr_p, 2, &code_unit))
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("decode error"));
}
parser_ctx_p->input_curr_p += 2;
out_token_p->value = code_unit;
}
else if (ch == LIT_CHAR_LOWERCASE_U
&& re_hex_lookup (parser_ctx_p, 4))
{
ecma_char_t code_unit;
if (!lit_read_code_unit_from_hex (parser_ctx_p->input_curr_p, 4, &code_unit))
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("decode error"));
}
parser_ctx_p->input_curr_p += 4;
out_token_p->value = code_unit;
}
else if (ch == LIT_CHAR_LOWERCASE_D)
{
out_token_p->type = RE_TOK_DIGIT;
break;
}
else if (ch == LIT_CHAR_UPPERCASE_D)
{
out_token_p->type = RE_TOK_NOT_DIGIT;
break;
}
else if (ch == LIT_CHAR_LOWERCASE_S)
{
out_token_p->type = RE_TOK_WHITE;
break;
}
else if (ch == LIT_CHAR_UPPERCASE_S)
{
out_token_p->type = RE_TOK_NOT_WHITE;
break;
}
else if (ch == LIT_CHAR_LOWERCASE_W)
{
out_token_p->type = RE_TOK_WORD_CHAR;
break;
}
else if (ch == LIT_CHAR_UPPERCASE_W)
{
out_token_p->type = RE_TOK_NOT_WORD_CHAR;
break;
}
else if (lit_char_is_decimal_digit (ch))
{
if (ch == LIT_CHAR_0)
{
if (parser_ctx_p->input_curr_p < parser_ctx_p->input_end_p
&& lit_char_is_decimal_digit (*parser_ctx_p->input_curr_p))
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("RegExp escape pattern error."));
}
out_token_p->value = LIT_UNICODE_CODE_POINT_NULL;
}
else
{
if (parser_ctx_p->num_of_groups == -1)
{
re_count_num_of_groups (parser_ctx_p);
}
if (parser_ctx_p->num_of_groups)
{
parser_ctx_p->input_curr_p--;
uint32_t number = 0;
int index = 0;
do
{
if (index >= RE_MAX_RE_DECESC_DIGITS)
{
ret_value = ecma_raise_syntax_error (ECMA_ERR_MSG ("RegExp escape error: decimal escape too long."));
return ret_value;
}
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
break;
}
ecma_char_t digit = *parser_ctx_p->input_curr_p++;
if (!lit_char_is_decimal_digit (digit))
{
parser_ctx_p->input_curr_p--;
break;
}
number = number * 10 + lit_char_hex_to_int (digit);
index++;
}
while (true);
if ((int) number <= parser_ctx_p->num_of_groups)
{
out_token_p->type = RE_TOK_BACKREFERENCE;
}
else
/* Invalid backreference, fallback to octal */
{
/* Rewind to start of number. */
parser_ctx_p->input_curr_p -= index;
/* Try to reparse as octal. */
ecma_char_t digit = *parser_ctx_p->input_curr_p;
if (!lit_char_is_octal_digit (digit))
{
/* Not octal, keep digit character value. */
number = digit;
parser_ctx_p->input_curr_p++;
}
else
{
number = re_parse_octal (parser_ctx_p);
}
}
out_token_p->value = number;
}
else
/* Invalid backreference, fallback to octal if possible */
{
if (!lit_char_is_octal_digit (ch))
{
/* Not octal, keep character value. */
out_token_p->value = ch;
}
else
{
parser_ctx_p->input_curr_p--;
out_token_p->value = re_parse_octal (parser_ctx_p);
}
}
}
}
else
{
out_token_p->value = ch;
}
ret_value = re_parse_iterator (parser_ctx_p, out_token_p);
break;
}
case LIT_CHAR_LEFT_PAREN:
{
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("Unterminated group"));
}
if (*parser_ctx_p->input_curr_p == LIT_CHAR_QUESTION)
{
parser_ctx_p->input_curr_p++;
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("Invalid group"));
}
ch = *parser_ctx_p->input_curr_p++;
if (ch == LIT_CHAR_EQUALS)
{
/* (?= */
out_token_p->type = RE_TOK_ASSERT_START_POS_LOOKAHEAD;
}
else if (ch == LIT_CHAR_EXCLAMATION)
{
/* (?! */
out_token_p->type = RE_TOK_ASSERT_START_NEG_LOOKAHEAD;
}
else if (ch == LIT_CHAR_COLON)
{
/* (?: */
out_token_p->type = RE_TOK_START_NON_CAPTURE_GROUP;
}
else
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("Invalid group"));
}
}
else
{
/* ( */
out_token_p->type = RE_TOK_START_CAPTURE_GROUP;
}
break;
}
case LIT_CHAR_RIGHT_PAREN:
{
out_token_p->type = RE_TOK_END_GROUP;
ret_value = re_parse_iterator (parser_ctx_p, out_token_p);
break;
}
case LIT_CHAR_LEFT_SQUARE:
{
out_token_p->type = RE_TOK_START_CHAR_CLASS;
if (parser_ctx_p->input_curr_p >= parser_ctx_p->input_end_p)
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("invalid character class"));
}
if (*parser_ctx_p->input_curr_p == LIT_CHAR_CIRCUMFLEX)
{
out_token_p->type = RE_TOK_START_INV_CHAR_CLASS;
parser_ctx_p->input_curr_p++;
}
break;
}
case LIT_CHAR_QUESTION:
case LIT_CHAR_ASTERISK:
case LIT_CHAR_PLUS:
case LIT_CHAR_LEFT_BRACE:
{
return ecma_raise_syntax_error (ECMA_ERR_MSG ("Invalid RegExp token."));
}
case LIT_CHAR_NULL:
{
out_token_p->type = RE_TOK_EOF;
break;
}
default:
{
out_token_p->type = RE_TOK_CHAR;
out_token_p->value = ch;
ret_value = re_parse_iterator (parser_ctx_p, out_token_p);
break;
}
}
return ret_value;
} /* re_parse_next_token */
/**
* @}
* @}
* @}
*/
#endif /* !CONFIG_DISABLE_REGEXP_BUILTIN */

View file

@ -0,0 +1,117 @@
/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
* Copyright 2015-2016 University of Szeged.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RE_PARSER_H
#define RE_PARSER_H
#ifndef CONFIG_DISABLE_REGEXP_BUILTIN
/** \addtogroup parser Parser
* @{
*
* \addtogroup regexparser Regular expression
* @{
*
* \addtogroup regexparser_bytecode Bytecode
* @{
*/
/**
* RegExp token type definitions
*/
typedef enum
{
RE_TOK_EOF, /**< EOF */
RE_TOK_BACKREFERENCE, /**< "\[0..9]" */
RE_TOK_CHAR, /**< any character */
RE_TOK_ALTERNATIVE, /**< "|" */
RE_TOK_ASSERT_START, /**< "^" */
RE_TOK_ASSERT_END, /**< "$" */
RE_TOK_PERIOD, /**< "." */
RE_TOK_START_CAPTURE_GROUP, /**< "(" */
RE_TOK_START_NON_CAPTURE_GROUP, /**< "(?:" */
RE_TOK_END_GROUP, /**< ")" */
RE_TOK_ASSERT_START_POS_LOOKAHEAD, /**< "(?=" */
RE_TOK_ASSERT_START_NEG_LOOKAHEAD, /**< "(?!" */
RE_TOK_ASSERT_WORD_BOUNDARY, /**< "\b" */
RE_TOK_ASSERT_NOT_WORD_BOUNDARY, /**< "\B" */
RE_TOK_DIGIT, /**< "\d" */
RE_TOK_NOT_DIGIT, /**< "\D" */
RE_TOK_WHITE, /**< "\s" */
RE_TOK_NOT_WHITE, /**< "\S" */
RE_TOK_WORD_CHAR, /**< "\w" */
RE_TOK_NOT_WORD_CHAR, /**< "\W" */
RE_TOK_START_CHAR_CLASS, /**< "[ ]" */
RE_TOK_START_INV_CHAR_CLASS, /**< "[^ ]" */
} re_token_type_t;
/**
* @}
*
* \addtogroup regexparser_parser Parser
* @{
*/
/**
* RegExp constant of infinite
*/
#define RE_ITERATOR_INFINITE ((uint32_t) - 1)
/**
* Maximum number of decimal escape digits
*/
#define RE_MAX_RE_DECESC_DIGITS 9
/**
* RegExp token type
*/
typedef struct
{
re_token_type_t type; /**< type of the token */
uint32_t value; /**< value of the token */
uint32_t qmin; /**< minimum number of token iterations */
uint32_t qmax; /**< maximum number of token iterations */
bool greedy; /**< type of iteration */
} re_token_t;
/**
* RegExp parser context
*/
typedef struct
{
const lit_utf8_byte_t *input_start_p; /**< start of input pattern */
const lit_utf8_byte_t *input_curr_p; /**< current position in input pattern */
const lit_utf8_byte_t *input_end_p; /**< end of input pattern */
int num_of_groups; /**< number of groups */
uint32_t num_of_classes; /**< number of character classes */
} re_parser_ctx_t;
typedef void (*re_char_class_callback) (void *re_ctx_p, ecma_char_t start, ecma_char_t end);
ecma_value_t
re_parse_char_class (re_parser_ctx_t *, re_char_class_callback, void *, re_token_t *);
ecma_value_t
re_parse_next_token (re_parser_ctx_t *, re_token_t *);
/**
* @}
* @}
* @}
*/
#endif /* !CONFIG_DISABLE_REGEXP_BUILTIN */
#endif /* !RE_PARSER_H */