1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#include "common.h"
17425bb815Sopenharmony_ci#include "ecma-helpers.h"
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_ci#if ENABLED (JERRY_PARSER)
20425bb815Sopenharmony_ci
21425bb815Sopenharmony_ci/** \addtogroup parser Parser
22425bb815Sopenharmony_ci * @{
23425bb815Sopenharmony_ci *
24425bb815Sopenharmony_ci * \addtogroup jsparser JavaScript
25425bb815Sopenharmony_ci * @{
26425bb815Sopenharmony_ci *
27425bb815Sopenharmony_ci * \addtogroup jsparser_utils Utility
28425bb815Sopenharmony_ci * @{
29425bb815Sopenharmony_ci */
30425bb815Sopenharmony_ci
31425bb815Sopenharmony_ci/**
32425bb815Sopenharmony_ci * Free literal.
33425bb815Sopenharmony_ci */
34425bb815Sopenharmony_civoid
35425bb815Sopenharmony_ciutil_free_literal (lexer_literal_t *literal_p) /**< literal */
36425bb815Sopenharmony_ci{
37425bb815Sopenharmony_ci  if (literal_p->type == LEXER_IDENT_LITERAL
38425bb815Sopenharmony_ci      || literal_p->type == LEXER_STRING_LITERAL)
39425bb815Sopenharmony_ci  {
40425bb815Sopenharmony_ci    if (!(literal_p->status_flags & LEXER_FLAG_SOURCE_PTR))
41425bb815Sopenharmony_ci    {
42425bb815Sopenharmony_ci      jmem_heap_free_block ((void *) literal_p->u.char_p, literal_p->prop.length);
43425bb815Sopenharmony_ci    }
44425bb815Sopenharmony_ci  }
45425bb815Sopenharmony_ci  else if ((literal_p->type == LEXER_FUNCTION_LITERAL)
46425bb815Sopenharmony_ci           || (literal_p->type == LEXER_REGEXP_LITERAL))
47425bb815Sopenharmony_ci  {
48425bb815Sopenharmony_ci    ecma_bytecode_deref (literal_p->u.bytecode_p);
49425bb815Sopenharmony_ci  }
50425bb815Sopenharmony_ci} /* util_free_literal */
51425bb815Sopenharmony_ci
52425bb815Sopenharmony_ci#if ENABLED (JERRY_PARSER_DUMP_BYTE_CODE)
53425bb815Sopenharmony_ci
54425bb815Sopenharmony_ci/**
55425bb815Sopenharmony_ci * Debug utility to print a character sequence.
56425bb815Sopenharmony_ci */
57425bb815Sopenharmony_cistatic void
58425bb815Sopenharmony_ciutil_print_chars (const uint8_t *char_p, /**< character pointer */
59425bb815Sopenharmony_ci                  size_t size) /**< size */
60425bb815Sopenharmony_ci{
61425bb815Sopenharmony_ci  while (size > 0)
62425bb815Sopenharmony_ci  {
63425bb815Sopenharmony_ci    JERRY_DEBUG_MSG ("%c", *char_p++);
64425bb815Sopenharmony_ci    size--;
65425bb815Sopenharmony_ci  }
66425bb815Sopenharmony_ci} /* util_print_chars */
67425bb815Sopenharmony_ci
68425bb815Sopenharmony_ci/**
69425bb815Sopenharmony_ci * Debug utility to print a number.
70425bb815Sopenharmony_ci */
71425bb815Sopenharmony_cistatic void
72425bb815Sopenharmony_ciutil_print_number (ecma_number_t num_p) /**< number to print */
73425bb815Sopenharmony_ci{
74425bb815Sopenharmony_ci  lit_utf8_byte_t str_buf[ECMA_MAX_CHARS_IN_STRINGIFIED_NUMBER];
75425bb815Sopenharmony_ci  lit_utf8_size_t str_size = ecma_number_to_utf8_string (num_p, str_buf, sizeof (str_buf));
76425bb815Sopenharmony_ci  str_buf[str_size] = 0;
77425bb815Sopenharmony_ci  JERRY_DEBUG_MSG ("%s", str_buf);
78425bb815Sopenharmony_ci} /* util_print_number */
79425bb815Sopenharmony_ci
80425bb815Sopenharmony_ci/**
81425bb815Sopenharmony_ci * Print literal.
82425bb815Sopenharmony_ci */
83425bb815Sopenharmony_civoid
84425bb815Sopenharmony_ciutil_print_literal (lexer_literal_t *literal_p) /**< literal */
85425bb815Sopenharmony_ci{
86425bb815Sopenharmony_ci  if (literal_p->type == LEXER_IDENT_LITERAL)
87425bb815Sopenharmony_ci  {
88425bb815Sopenharmony_ci    JERRY_DEBUG_MSG ("ident(");
89425bb815Sopenharmony_ci    util_print_chars (literal_p->u.char_p, literal_p->prop.length);
90425bb815Sopenharmony_ci  }
91425bb815Sopenharmony_ci  else if (literal_p->type == LEXER_FUNCTION_LITERAL)
92425bb815Sopenharmony_ci  {
93425bb815Sopenharmony_ci    JERRY_DEBUG_MSG ("function");
94425bb815Sopenharmony_ci    return;
95425bb815Sopenharmony_ci  }
96425bb815Sopenharmony_ci  else if (literal_p->type == LEXER_STRING_LITERAL)
97425bb815Sopenharmony_ci  {
98425bb815Sopenharmony_ci    JERRY_DEBUG_MSG ("string(");
99425bb815Sopenharmony_ci    util_print_chars (literal_p->u.char_p, literal_p->prop.length);
100425bb815Sopenharmony_ci  }
101425bb815Sopenharmony_ci  else if (literal_p->type == LEXER_NUMBER_LITERAL)
102425bb815Sopenharmony_ci  {
103425bb815Sopenharmony_ci    JERRY_DEBUG_MSG ("number(");
104425bb815Sopenharmony_ci    util_print_number (ecma_get_number_from_value (literal_p->u.value));
105425bb815Sopenharmony_ci  }
106425bb815Sopenharmony_ci  else if (literal_p->type == LEXER_REGEXP_LITERAL)
107425bb815Sopenharmony_ci  {
108425bb815Sopenharmony_ci    JERRY_DEBUG_MSG ("regexp");
109425bb815Sopenharmony_ci    return;
110425bb815Sopenharmony_ci  }
111425bb815Sopenharmony_ci  else
112425bb815Sopenharmony_ci  {
113425bb815Sopenharmony_ci    JERRY_DEBUG_MSG ("unknown");
114425bb815Sopenharmony_ci    return;
115425bb815Sopenharmony_ci  }
116425bb815Sopenharmony_ci
117425bb815Sopenharmony_ci  JERRY_DEBUG_MSG (")");
118425bb815Sopenharmony_ci} /* util_print_literal */
119425bb815Sopenharmony_ci
120425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_PARSER_DUMP_BYTE_CODE) */
121425bb815Sopenharmony_ci
122425bb815Sopenharmony_ci/**
123425bb815Sopenharmony_ci * @}
124425bb815Sopenharmony_ci * @}
125425bb815Sopenharmony_ci * @}
126425bb815Sopenharmony_ci */
127425bb815Sopenharmony_ci
128425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_PARSER) */
129