Skip to content

Commit f8dff65

Browse files
Support syntax error feedback in parser.
Now, parser correctly finishes parse procedure if syntax of source code is incorrect (syntax correctness is indicated using return value): - parser-internal memory management is performed using jsp_mm_alloc / jsp_mm_free; - upon detection of incorrect syntax, all parser-allocated memory regions are deallocated using jsp_mm_free_all and parse finishes with corresponding return value. JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan [email protected]
1 parent 0818856 commit f8dff65

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+203
-155
lines changed

jerry-core/jerry.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ typedef enum
4949
{
5050
ERR_OUT_OF_MEMORY = 10,
5151
ERR_SYSCALL = 11,
52-
ERR_PARSER = 12,
5352
ERR_UNIMPLEMENTED_CASE = 118,
5453
ERR_FAILED_INTERNAL_ASSERTION = 120
5554
} jerry_fatal_code_t;

jerry-core/jrt/jrt-fatals.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ jerry_fatal (jerry_fatal_code_t code) /**< status code */
4747
/* print nothing as it may invoke syscall recursively */
4848
break;
4949
}
50-
case ERR_PARSER:
51-
{
52-
printf ("ERR_PARSER\n");
53-
break;
54-
}
5550
case ERR_UNIMPLEMENTED_CASE:
5651
{
5752
printf ("ERR_UNIMPLEMENTED_CASE\n");

jerry-core/jrt/jrt-libc-includes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#define JRT_LIBC_INCLUDES_H
1818

1919
#include <ctype.h>
20+
#include <setjmp.h>
21+
#include <stdarg.h>
2022
#include <stdio.h>
2123
#include <stdlib.h>
2224
#include <string.h>

jerry-core/parser/js/collections/array-list.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
*/
1515

1616
#include "array-list.h"
17-
#include "mem-heap.h"
1817
#include "jrt-libc-includes.h"
18+
#include "jsp-mm.h"
1919

2020
typedef struct
2121
{
@@ -44,14 +44,14 @@ array_list_append (array_list al, void *element)
4444
array_list_header *h = extract_header (al);
4545
if ((h->len + 1) * h->element_size + sizeof (array_list_header) > h->size)
4646
{
47-
size_t size = mem_heap_recommend_allocation_size (h->size + h->element_size);
47+
size_t size = jsp_mm_recommend_size (h->size + h->element_size);
4848
JERRY_ASSERT (size > h->size);
4949

50-
uint8_t* new_block_p = (uint8_t*) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_SHORT_TERM);
50+
uint8_t *new_block_p = (uint8_t *) jsp_mm_alloc (size);
5151
memcpy (new_block_p, h, h->size);
5252
memset (new_block_p + h->size, 0, size - h->size);
5353

54-
mem_heap_free_block ((uint8_t *) h);
54+
jsp_mm_free (h);
5555

5656
h = (array_list_header *) new_block_p;
5757
h->size = size;
@@ -111,8 +111,8 @@ array_list_set_last_element (array_list al, size_t index, void *elem)
111111
array_list
112112
array_list_init (uint8_t element_size)
113113
{
114-
size_t size = mem_heap_recommend_allocation_size (sizeof (array_list_header));
115-
array_list_header *header = (array_list_header *) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_SHORT_TERM);
114+
size_t size = jsp_mm_recommend_size (sizeof (array_list_header));
115+
array_list_header *header = (array_list_header *) jsp_mm_alloc (size);
116116
memset (header, 0, size);
117117
header->element_size = element_size;
118118
header->len = 0;
@@ -131,5 +131,5 @@ void
131131
array_list_free (array_list al)
132132
{
133133
array_list_header *h = extract_header (al);
134-
mem_heap_free_block ((uint8_t *) h);
134+
jsp_mm_free (h);
135135
}

jerry-core/parser/js/collections/hash-table.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* limitations under the License.
1414
*/
1515

16-
#include "hash-table.h"
1716
#include "array-list.h"
18-
#include "mem-heap.h"
17+
#include "hash-table.h"
1918
#include "jrt-libc-includes.h"
19+
#include "jsp-mm.h"
2020

2121
typedef struct
2222
{
@@ -25,7 +25,6 @@ typedef struct
2525
uint16_t size;
2626
uint8_t key_size;
2727
uint8_t value_size;
28-
mem_heap_alloc_term_t alloc_term;
2928
} hash_table_int;
3029

3130
static hash_table_int *
@@ -65,12 +64,12 @@ hash_table_insert (hash_table ht, void *key, void *value)
6564
{
6665
list = array_list_init (bucket_size (hti));
6766
}
68-
uint8_t *bucket = (uint8_t*) mem_heap_alloc_block (bucket_size (hti), hti->alloc_term);
67+
uint8_t *bucket = (uint8_t *) jsp_mm_alloc (bucket_size (hti));
6968
memcpy (bucket, key, hti->key_size);
7069
memcpy (bucket + hti->key_size, value, hti->value_size);
7170
list = array_list_append (list, bucket);
7271
hti->data[index] = list;
73-
mem_heap_free_block (bucket);
72+
jsp_mm_free (bucket);
7473
}
7574

7675
void *
@@ -98,15 +97,14 @@ hash_table_lookup (hash_table ht, void *key)
9897

9998
hash_table
10099
hash_table_init (uint8_t key_size, uint8_t value_size, uint16_t size,
101-
uint16_t (*hash) (void *), mem_heap_alloc_term_t alloc_term)
100+
uint16_t (*hash) (void *))
102101
{
103-
hash_table_int *res = (hash_table_int *) mem_heap_alloc_block (sizeof (hash_table_int), alloc_term);
102+
hash_table_int *res = (hash_table_int *) jsp_mm_alloc (sizeof (hash_table_int));
104103
memset (res, 0, sizeof (hash_table_int));
105104
res->key_size = key_size;
106105
res->value_size = value_size;
107106
res->size = size;
108-
res->alloc_term = alloc_term;
109-
res->data = (array_list *) mem_heap_alloc_block (size * sizeof (array_list), alloc_term);
107+
res->data = (array_list *) jsp_mm_alloc (size * sizeof (array_list));
110108
memset (res->data, 0, size * sizeof (array_list));
111109
res->hash = hash;
112110
return res;
@@ -125,6 +123,6 @@ hash_table_free (hash_table ht)
125123
set_list (h, i, null_list);
126124
}
127125
}
128-
mem_heap_free_block ((uint8_t *) h->data);
129-
mem_heap_free_block ((uint8_t *) h);
126+
jsp_mm_free (h->data);
127+
jsp_mm_free (h);
130128
}

jerry-core/parser/js/collections/hash-table.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
typedef void* hash_table;
3131
#define null_hash NULL
3232

33-
hash_table hash_table_init (uint8_t, uint8_t, uint16_t, uint16_t (*hash) (void *), mem_heap_alloc_term_t);
33+
hash_table hash_table_init (uint8_t, uint8_t, uint16_t, uint16_t (*hash) (void *));
3434
void hash_table_free (hash_table);
3535
void hash_table_insert (hash_table, void *, void *);
3636
void *hash_table_lookup (hash_table, void *);

jerry-core/parser/js/collections/linked-list.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
* limitations under the License.
1414
*/
1515

16-
#include "linked-list.h"
1716
#include "jrt-libc-includes.h"
18-
#include "mem-heap.h"
17+
#include "jsp-mm.h"
18+
#include "linked-list.h"
1919

2020
typedef struct linked_list_header
2121
{
@@ -31,14 +31,14 @@ do { \
3131

3232
static size_t linked_list_block_size (uint16_t element_size)
3333
{
34-
return mem_heap_recommend_allocation_size (sizeof (linked_list_header) + element_size) - sizeof (linked_list_header);
34+
return jsp_mm_recommend_size (sizeof (linked_list_header) + element_size) - sizeof (linked_list_header);
3535
}
3636

3737
linked_list
3838
linked_list_init (uint16_t element_size)
3939
{
4040
size_t size = sizeof (linked_list_header) + linked_list_block_size (element_size);
41-
linked_list list = (linked_list) mem_heap_alloc_block (size, MEM_HEAP_ALLOC_SHORT_TERM);
41+
linked_list list = (linked_list) jsp_mm_alloc (size);
4242
if (list == null_list)
4343
{
4444
printf ("Out of memory");
@@ -60,7 +60,7 @@ linked_list_free (linked_list list)
6060
{
6161
linked_list_free ((linked_list) header->next);
6262
}
63-
mem_heap_free_block (list);
63+
jsp_mm_free (list);
6464
}
6565

6666
void *

jerry-core/parser/js/lexer.cpp

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
* limitations under the License.
1414
*/
1515

16-
#include "mem-allocator.h"
16+
#include "ecma-helpers.h"
1717
#include "jrt-libc-includes.h"
18+
#include "jsp-mm.h"
1819
#include "lexer.h"
1920
#include "syntax-errors.h"
20-
#include "ecma-helpers.h"
2121

2222
static token saved_token, prev_token, sent_token, empty_token;
2323

@@ -474,9 +474,7 @@ convert_string_to_token_transform_escape_seq (token_type tok_type, /**< type of
474474
JERRY_ASSERT (source_str_p != NULL);
475475
}
476476

477-
MEM_DEFINE_LOCAL_ARRAY (str_buf_p,
478-
source_str_size,
479-
ecma_char_t);
477+
ecma_char_t *str_buf_p = (ecma_char_t*) jsp_mm_alloc (source_str_size * sizeof (ecma_char_t));
480478

481479
const char *source_str_iter_p = source_str_p;
482480
ecma_char_t *str_buf_iter_p = str_buf_p;
@@ -632,7 +630,7 @@ convert_string_to_token_transform_escape_seq (token_type tok_type, /**< type of
632630
PARSE_ERROR ("Malformed escape sequence", source_str_p - buffer_start);
633631
}
634632

635-
MEM_FINALIZE_LOCAL_ARRAY (str_buf_p);
633+
jsp_mm_free (str_buf_p);
636634

637635
return ret;
638636
} /* convert_string_to_token_transform_escape_seq */
@@ -860,13 +858,12 @@ parse_number (void)
860858
tok_length = (size_t) (buffer - token_start);;
861859
if (is_fp || is_exp)
862860
{
863-
ecma_char_t *temp = (ecma_char_t*) mem_heap_alloc_block ((size_t) (tok_length + 1),
864-
MEM_HEAP_ALLOC_SHORT_TERM);
861+
ecma_char_t *temp = (ecma_char_t*) jsp_mm_alloc ((size_t) (tok_length + 1) * sizeof (ecma_char_t));
865862
strncpy ((char *) temp, token_start, (size_t) (tok_length));
866863
temp[tok_length] = '\0';
867864
ecma_number_t res = ecma_zt_string_to_number (temp);
868865
JERRY_ASSERT (!ecma_number_is_nan (res));
869-
mem_heap_free_block (temp);
866+
jsp_mm_free (temp);
870867
known_token = convert_seen_num_to_token (res);
871868
token_start = NULL;
872869
return known_token;
@@ -1467,28 +1464,22 @@ lexer_are_tokens_with_same_identifier (token id1, /**< identifier token (TOK_NAM
14671464
} /* lexer_are_tokens_with_same_identifier */
14681465

14691466
/**
1470-
* Initialize lexer to start parsing of a new source
1467+
* Intitialize lexer
14711468
*/
14721469
void
1473-
lexer_init_source (const char *source, /**< script source */
1474-
size_t source_size) /**< script source size in bytes */
1470+
lexer_init (const char *source, /**< script source */
1471+
size_t source_size /**< script source size in bytes */,
1472+
bool show_opcodes) /**< flag indicating if to dump opcodes */
14751473
{
1474+
empty_token.type = TOK_EMPTY;
1475+
empty_token.uid = 0;
1476+
empty_token.loc = 0;
1477+
14761478
saved_token = prev_token = sent_token = empty_token;
14771479

14781480
buffer_size = source_size;
14791481
lexer_set_source (source);
14801482
lexer_set_strict_mode (false);
1481-
} /* lexer_init_source */
1482-
1483-
/**
1484-
* Intitialize lexer
1485-
*/
1486-
void
1487-
lexer_init (bool show_opcodes) /**< flag indicating if to dump opcodes */
1488-
{
1489-
empty_token.type = TOK_EMPTY;
1490-
empty_token.uid = 0;
1491-
empty_token.loc = 0;
14921483

14931484
#ifndef JERRY_NDEBUG
14941485
allow_dump_lines = show_opcodes;
@@ -1497,8 +1488,3 @@ lexer_init (bool show_opcodes) /**< flag indicating if to dump opcodes */
14971488
allow_dump_lines = false;
14981489
#endif /* JERRY_NDEBUG */
14991490
} /* lexer_init */
1500-
1501-
void
1502-
lexer_free (void)
1503-
{
1504-
}

jerry-core/parser/js/lexer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,7 @@ typedef struct
169169
*/
170170
#define TOKEN_EMPTY_INITIALIZER {0, TOK_EMPTY, 0}
171171

172-
void lexer_init (bool);
173-
void lexer_init_source (const char *, size_t);
174-
void lexer_free (void);
172+
void lexer_init (const char *, size_t, bool);
175173

176174
token lexer_next_token (void);
177175
void lexer_save_token (token);

0 commit comments

Comments
 (0)