1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef JS_PARSER_H
17#define JS_PARSER_H
18
19#include "ecma-globals.h"
20
21/** \addtogroup parser Parser
22 * @{
23 *
24 * \addtogroup jsparser JavaScript
25 * @{
26 *
27 * \addtogroup jsparser_parser Parser
28 * @{
29 */
30
31/**
32 * Error codes.
33 */
34typedef enum
35{
36  PARSER_ERR_NO_ERROR,                                /**< no error */
37
38  PARSER_ERR_OUT_OF_MEMORY,                           /**< out of memory */
39  PARSER_ERR_LITERAL_LIMIT_REACHED,                   /**< maximum number of literals reached */
40  PARSER_ERR_SCOPE_STACK_LIMIT_REACHED,               /**< maximum depth of scope stack reached */
41  PARSER_ERR_ARGUMENT_LIMIT_REACHED,                  /**< maximum number of function arguments reached */
42  PARSER_ERR_STACK_LIMIT_REACHED,                     /**< maximum function stack size reached */
43  PARSER_ERR_JERRY_STACK_LIMIT_REACHED,               /**< maximum jerry context stack limit reached */
44
45  PARSER_ERR_INVALID_CHARACTER,                       /**< unexpected character */
46  PARSER_ERR_INVALID_OCTAL_DIGIT,                     /**< invalid octal digit */
47  PARSER_ERR_INVALID_HEX_DIGIT,                       /**< invalid hexadecimal digit */
48#if ENABLED (JERRY_ES2015)
49  PARSER_ERR_INVALID_BIN_DIGIT,                       /**< invalid binary digit */
50#endif /* ENABLED (JERRY_ES2015) */
51  PARSER_ERR_INVALID_ESCAPE_SEQUENCE,                 /**< invalid escape sequence */
52  PARSER_ERR_INVALID_UNICODE_ESCAPE_SEQUENCE,         /**< invalid unicode escape sequence */
53  PARSER_ERR_INVALID_IDENTIFIER_START,                /**< character cannot be start of an identifier */
54  PARSER_ERR_INVALID_IDENTIFIER_PART,                 /**< character cannot be part of an identifier */
55  PARSER_ERR_INVALID_KEYWORD,                         /**< escape sequences are not allowed in keywords */
56
57  PARSER_ERR_INVALID_NUMBER,                          /**< invalid number literal */
58  PARSER_ERR_MISSING_EXPONENT,                        /**< missing exponent */
59  PARSER_ERR_IDENTIFIER_AFTER_NUMBER,                 /**< identifier start after number */
60
61  PARSER_ERR_INVALID_REGEXP,                          /**< invalid regular expression */
62  PARSER_ERR_UNKNOWN_REGEXP_FLAG,                     /**< unknown regexp flag */
63  PARSER_ERR_DUPLICATED_REGEXP_FLAG,                  /**< duplicated regexp flag */
64  PARSER_ERR_UNSUPPORTED_REGEXP,                      /**< regular expression is not supported */
65
66  PARSER_ERR_IDENTIFIER_TOO_LONG,                     /**< too long identifier */
67  PARSER_ERR_STRING_TOO_LONG,                         /**< too long string literal */
68  PARSER_ERR_NUMBER_TOO_LONG,                         /**< too long number literal */
69  PARSER_ERR_REGEXP_TOO_LONG,                         /**< too long regexp literal */
70
71  PARSER_ERR_UNTERMINATED_MULTILINE_COMMENT,          /**< unterminated multiline comment */
72  PARSER_ERR_UNTERMINATED_STRING,                     /**< unterminated string literal */
73  PARSER_ERR_UNTERMINATED_REGEXP,                     /**< unterminated regexp literal */
74
75  PARSER_ERR_NEWLINE_NOT_ALLOWED,                     /**< newline is not allowed */
76  PARSER_ERR_OCTAL_NUMBER_NOT_ALLOWED,                /**< octal numbers are not allowed in strict mode */
77  PARSER_ERR_OCTAL_ESCAPE_NOT_ALLOWED,                /**< octal escape sequences are not allowed in strict mode */
78  PARSER_ERR_STRICT_IDENT_NOT_ALLOWED,                /**< identifier name is reserved in strict mode */
79  PARSER_ERR_EVAL_NOT_ALLOWED,                        /**< eval is not allowed here in strict mode */
80  PARSER_ERR_ARGUMENTS_NOT_ALLOWED,                   /**< arguments is not allowed here in strict mode */
81#if ENABLED (JERRY_ES2015)
82  PARSER_ERR_USE_STRICT_NOT_ALLOWED,                  /**< use strict directive is not allowed */
83  PARSER_ERR_YIELD_NOT_ALLOWED,                       /**< yield expression is not allowed */
84  PARSER_ERR_AWAIT_NOT_ALLOWED,                       /**< await expression is not allowed */
85  PARSER_ERR_FOR_IN_OF_DECLARATION,                   /**< variable declaration in for-in or for-of loop */
86  PARSER_ERR_DUPLICATED_PROTO,                        /**< duplicated __proto__ fields are not allowed */
87#endif /* ENABLED (JERRY_ES2015) */
88  PARSER_ERR_DELETE_IDENT_NOT_ALLOWED,                /**< identifier delete is not allowed in strict mode */
89  PARSER_ERR_EVAL_CANNOT_ASSIGNED,                    /**< eval cannot be assigned in strict mode */
90  PARSER_ERR_ARGUMENTS_CANNOT_ASSIGNED,               /**< arguments cannot be assigned in strict mode */
91  PARSER_ERR_WITH_NOT_ALLOWED,                        /**< with statement is not allowed in strict mode */
92  PARSER_ERR_MULTIPLE_DEFAULTS_NOT_ALLOWED,           /**< multiple default cases are not allowed */
93  PARSER_ERR_DEFAULT_NOT_IN_SWITCH,                   /**< default statement is not in switch block */
94  PARSER_ERR_CASE_NOT_IN_SWITCH,                      /**< case statement is not in switch block */
95
96  PARSER_ERR_LEFT_PAREN_EXPECTED,                     /**< left paren expected */
97  PARSER_ERR_LEFT_BRACE_EXPECTED,                     /**< left brace expected */
98  PARSER_ERR_RIGHT_PAREN_EXPECTED,                    /**< right paren expected */
99  PARSER_ERR_RIGHT_SQUARE_EXPECTED,                   /**< right square expected */
100
101  PARSER_ERR_COLON_EXPECTED,                          /**< colon expected */
102  PARSER_ERR_COLON_FOR_CONDITIONAL_EXPECTED,          /**< colon expected for conditional expression */
103  PARSER_ERR_SEMICOLON_EXPECTED,                      /**< semicolon expected */
104  PARSER_ERR_IN_EXPECTED,                             /**< in keyword expected */
105  PARSER_ERR_WHILE_EXPECTED,                          /**< while expected for do-while loop */
106  PARSER_ERR_CATCH_FINALLY_EXPECTED,                  /**< catch or finally expected */
107  PARSER_ERR_ARRAY_ITEM_SEPARATOR_EXPECTED,           /**< array item separator expected */
108  PARSER_ERR_OBJECT_ITEM_SEPARATOR_EXPECTED,          /**< object item separator expected */
109  PARSER_ERR_IDENTIFIER_EXPECTED,                     /**< identifier expected */
110  PARSER_ERR_EXPRESSION_EXPECTED,                     /**< expression expected */
111  PARSER_ERR_PRIMARY_EXP_EXPECTED,                    /**< primary expression expected */
112  PARSER_ERR_LEFT_HAND_SIDE_EXP_EXPECTED,             /**< left-hand-side expression expected */
113  PARSER_ERR_STATEMENT_EXPECTED,                      /**< statement expected */
114  PARSER_ERR_PROPERTY_IDENTIFIER_EXPECTED,            /**< property identifier expected */
115  PARSER_ERR_ARGUMENT_LIST_EXPECTED,                  /**< argument list expected */
116  PARSER_ERR_NO_ARGUMENTS_EXPECTED,                   /**< property getters must have no arguments */
117  PARSER_ERR_ONE_ARGUMENT_EXPECTED,                   /**< property setters must have one argument */
118
119  PARSER_ERR_INVALID_EXPRESSION,                      /**< invalid expression */
120  PARSER_ERR_INVALID_SWITCH,                          /**< invalid switch body */
121  PARSER_ERR_INVALID_BREAK,                           /**< break must be inside a loop or switch */
122  PARSER_ERR_INVALID_BREAK_LABEL,                     /**< break target not found */
123  PARSER_ERR_INVALID_CONTINUE,                        /**< continue must be inside a loop */
124  PARSER_ERR_INVALID_CONTINUE_LABEL,                  /**< continue target not found */
125  PARSER_ERR_INVALID_RETURN,                          /**< return must be inside a function */
126  PARSER_ERR_INVALID_RIGHT_SQUARE,                    /**< right square must terminate a block */
127  PARSER_ERR_DUPLICATED_LABEL,                        /**< duplicated label */
128  PARSER_ERR_OBJECT_PROPERTY_REDEFINED,               /**< property of object literal redefined */
129#if ENABLED (JERRY_ES2015)
130  PARSER_ERR_VARIABLE_REDECLARED,                     /**< a variable redeclared */
131  PARSER_ERR_LEXICAL_SINGLE_STATEMENT,                /**< lexical declaration in single statement context */
132  PARSER_ERR_LABELLED_FUNC_NOT_IN_BLOCK,              /**< labelled functions are only allowed inside blocks */
133  PARSER_ERR_LEXICAL_LET_BINDING,                     /**< let binding cannot be declared in let/const */
134  PARSER_ERR_MISSING_ASSIGN_AFTER_CONST,              /**< an assignment is required after a const declaration */
135
136  PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTORS,             /**< multiple class constructor */
137  PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR,           /**< class constructor cannot be an accessor */
138  PARSER_ERR_CLASS_CONSTRUCTOR_AS_GENERATOR,          /**< class constructor cannot be a generator */
139  PARSER_ERR_CLASS_STATIC_PROTOTYPE,                  /**< static method name 'prototype' is not allowed */
140  PARSER_ERR_UNEXPECTED_SUPER_KEYWORD,                /**< unexpected super keyword */
141
142  PARSER_ERR_RIGHT_BRACE_EXPECTED,                    /**< right brace expected */
143  PARSER_ERR_OF_EXPECTED,                             /**< of keyword expected */
144
145  PARSER_ERR_ASSIGNMENT_EXPECTED,                     /**< assignment expression expected */
146  PARSER_ERR_FORMAL_PARAM_AFTER_REST_PARAMETER,       /**< formal parameter after rest parameter */
147  PARSER_ERR_SETTER_REST_PARAMETER,                   /**< setter rest parameter */
148  PARSER_ERR_REST_PARAMETER_DEFAULT_INITIALIZER,      /**< rest parameter default initializer */
149  PARSER_ERR_DUPLICATED_ARGUMENT_NAMES,               /**< duplicated argument names */
150  PARSER_ERR_INVALID_DESTRUCTURING_PATTERN,           /**< invalid destructuring pattern */
151  PARSER_ERR_ILLEGAL_PROPERTY_IN_DECLARATION,         /**< illegal property in declaration context */
152  PARSER_ERR_INVALID_EXPONENTIATION,                  /**< left operand of ** operator cannot be unary expression */
153  PARSER_ERR_NEW_TARGET_EXPECTED,                     /**< expected new.target expression */
154  PARSER_ERR_NEW_TARGET_NOT_ALLOWED,                  /**< new.target is not allowed in the given context */
155#endif /* ENABLED (JERRY_ES2015) */
156#if ENABLED (JERRY_ES2015_MODULE_SYSTEM)
157  PARSER_ERR_FILE_NOT_FOUND,                          /**< file not found*/
158  PARSER_ERR_FROM_EXPECTED,                           /**< from expected */
159  PARSER_ERR_FROM_COMMA_EXPECTED,                     /**< from or comma expected */
160  PARSER_ERR_AS_EXPECTED,                             /**< as expected */
161  PARSER_ERR_STRING_EXPECTED,                         /**< string literal expected */
162  PARSER_ERR_MODULE_UNEXPECTED,                       /**< unexpected import or export statement */
163  PARSER_ERR_LEFT_BRACE_MULTIPLY_LITERAL_EXPECTED,    /**< left brace or multiply or literal expected */
164  PARSER_ERR_LEFT_BRACE_MULTIPLY_EXPECTED,            /**< left brace or multiply expected */
165  PARSER_ERR_RIGHT_BRACE_COMMA_EXPECTED,              /**< right brace or comma expected */
166  PARSER_ERR_DUPLICATED_EXPORT_IDENTIFIER,            /**< duplicated export identifier name */
167  PARSER_ERR_DUPLICATED_IMPORT_BINDING,               /**< duplicated import binding name */
168#endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
169
170  PARSER_ERR_NON_STRICT_ARG_DEFINITION                /**< non-strict argument definition */
171} parser_error_t;
172
173/**
174 * Source code line counter type.
175 */
176typedef uint32_t parser_line_counter_t;
177
178/**
179 * Error code location.
180 */
181typedef struct
182{
183  parser_error_t error;                               /**< error code */
184  parser_line_counter_t line;                         /**< line where the error occured */
185  parser_line_counter_t column;                       /**< column where the error occured */
186} parser_error_location_t;
187
188/* Note: source must be a valid UTF-8 string */
189ecma_value_t parser_parse_script (const uint8_t *arg_list_p, size_t arg_list_size,
190                                  const uint8_t *source_p, size_t source_size,
191                                  uint32_t parse_opts, ecma_compiled_code_t **bytecode_data_p);
192
193#if ENABLED (JERRY_ERROR_MESSAGES)
194const char *parser_error_to_string (parser_error_t);
195#endif /* ENABLED (JERRY_ERROR_MESSAGES) */
196
197/**
198 * @}
199 * @}
200 * @}
201 */
202
203#endif /* !JS_PARSER_H */
204