Skip to content

Commit 7025f97

Browse files
Adding routine for conversion of any character sequence (not only strings contained in source code buffer) to lexer token.
JerryScript-DCO-1.0-Signed-off-by: Ruben Ayrapetyan [email protected]
1 parent da7e9d9 commit 7025f97

File tree

3 files changed

+52
-28
lines changed

3 files changed

+52
-28
lines changed

jerry-core/parser/js/lexer.cpp

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -118,34 +118,38 @@ current_token_equals_to (const char *str)
118118
return false;
119119
}
120120

121+
/**
122+
* Compare specified string to literal
123+
*
124+
* @return true - if the literal contains exactly the specified string,
125+
* false - otherwise.
126+
*/
121127
static bool
122-
current_token_equals_to_literal (literal lit)
128+
string_equals_to_literal (const ecma_char_t *str_p, /**< characters buffer */
129+
ecma_length_t length, /**< string's length */
130+
literal lit) /**< literal */
123131
{
124132
if (lit.type == LIT_STR)
125133
{
126-
if (lit.data.lp.length != (ecma_length_t) (buffer - token_start))
127-
{
128-
return false;
129-
}
130-
if (!strncmp ((const char *) lit.data.lp.str, token_start, lit.data.lp.length))
134+
if (lit.data.lp.length == length
135+
&& strncmp ((const char *) lit.data.lp.str, (const char*) str_p, length) == 0)
131136
{
132137
return true;
133138
}
134139
}
135140
else if (lit.type == LIT_MAGIC_STR)
136141
{
137-
const char *str = (const char *) ecma_get_magic_string_zt (lit.data.magic_str_id);
138-
if (strlen (str) != (size_t) (buffer - token_start))
139-
{
140-
return false;
141-
}
142-
if (!strncmp (str, token_start, strlen (str)))
142+
const char *magic_str_p = (const char *) ecma_get_magic_string_zt (lit.data.magic_str_id);
143+
144+
if (strlen (magic_str_p) == length
145+
&& strncmp (magic_str_p, (const char*) str_p, length) == 0)
143146
{
144147
return true;
145148
}
146149
}
150+
147151
return false;
148-
}
152+
} /* string_equals_to_literal */
149153

150154
static literal
151155
adjust_string_ptrs (literal lit, size_t diff)
@@ -189,39 +193,53 @@ add_string_to_string_cache (const ecma_char_t* str, ecma_length_t length)
189193
return res;
190194
}
191195

192-
static literal
193-
add_current_token_to_string_cache (void)
194-
{
195-
return add_string_to_string_cache ((ecma_char_t*) token_start, (ecma_length_t) (buffer - token_start));
196-
}
197-
196+
/**
197+
* Convert string to token of specified type
198+
*
199+
* @return token descriptor
200+
*/
198201
static token
199-
convert_current_token_to_token (token_type tt)
202+
convert_string_to_token (token_type tt, /**< token type */
203+
const ecma_char_t *str_p, /**< characters buffer */
204+
ecma_length_t length) /**< string's length */
200205
{
201-
JERRY_ASSERT (token_start);
206+
JERRY_ASSERT (str_p != NULL);
202207

203208
for (literal_index_t i = 0; i < STACK_SIZE (literals); i++)
204209
{
205210
const literal lit = STACK_ELEMENT (literals, i);
206211
if ((lit.type == LIT_STR || lit.type == LIT_MAGIC_STR)
207-
&& current_token_equals_to_literal (lit))
212+
&& string_equals_to_literal (str_p, length, lit))
208213
{
209214
return create_token (tt, i);
210215
}
211216
}
212217

213-
literal lit = create_literal_from_str (token_start, (ecma_length_t) (buffer - token_start));
218+
literal lit = create_literal_from_str (str_p, length);
214219
JERRY_ASSERT (lit.type == LIT_STR || lit.type == LIT_MAGIC_STR);
215220
if (lit.type == LIT_STR)
216221
{
217-
lit = add_current_token_to_string_cache ();
222+
lit = add_string_to_string_cache (str_p, length);
218223
}
219224

220225
STACK_PUSH (literals, lit);
221226

222227
return create_token (tt, (literal_index_t) (STACK_SIZE (literals) - 1));
223228
}
224229

230+
/**
231+
* Convert string, currently processed by lexer (see also: token_start, buffer) to token of specified type
232+
*
233+
* @return token descriptor
234+
*/
235+
static token
236+
convert_current_token_to_token (token_type tt) /**< token type */
237+
{
238+
JERRY_ASSERT (token_start != NULL);
239+
240+
return convert_string_to_token (tt, (const ecma_char_t*) token_start, (ecma_length_t) (buffer - token_start));
241+
} /* convert_current_token_to_token */
242+
225243
/* If TOKEN represents a keyword, return decoded keyword,
226244
if TOKEN represents a Future Reserved Word, return KW_RESERVED,
227245
otherwise return KW_NONE. */

jerry-core/parser/js/literal.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ create_literal_from_num (ecma_number_t num)
3939
return ret;
4040
}
4141

42+
/**
43+
* Create literal from string
44+
*
45+
* @return literal descriptor
46+
*/
4247
literal
43-
create_literal_from_str (const char *s, ecma_length_t len)
48+
create_literal_from_str (const ecma_char_t *s, /**< characters buffer */
49+
ecma_length_t len) /**< string's length */
4450
{
45-
return create_literal_from_zt ((const ecma_char_t *) s, len);
46-
}
51+
return create_literal_from_zt (s, len);
52+
} /* create_literal_from_str */
4753

4854
literal
4955
create_literal_from_str_compute_len (const char *s)

jerry-core/parser/js/literal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ typedef struct
4343

4444
literal create_empty_literal (void);
4545
literal create_literal_from_num (ecma_number_t);
46-
literal create_literal_from_str (const char *, ecma_length_t);
46+
literal create_literal_from_str (const ecma_char_t*, ecma_length_t);
4747
literal create_literal_from_str_compute_len (const char *);
4848
literal create_literal_from_zt (const ecma_char_t *, ecma_length_t);
4949
bool literal_equal (literal, literal);

0 commit comments

Comments
 (0)