1 /*
2  *  Javascript normalizer.
3  *
4  *  Copyright (C) 2013-2022 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5  *  Copyright (C) 2008-2013 Sourcefire, Inc.
6  *
7  *  Authors: Török Edvin
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 as
11  *  published by the Free Software Foundation.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21  *  MA 02110-1301, USA.
22  */
23 #ifndef YYSTYPE
24 enum token_type {
25     TOK_FUTURE_RESERVED_WORD = 1,
26     TOK_ERROR,
27     TOK_IDENTIFIER_NAME,
28     TOK_TRUE,
29     TOK_FALSE,
30     TOK_NULL,
31     TOK_BRACKET_OPEN,
32     TOK_BRACKET_CLOSE,
33     TOK_COMMA,
34     TOK_CURLY_BRACE_OPEN,
35     TOK_CURLY_BRACE_CLOSE,
36     TOK_PAR_OPEN,
37     TOK_PAR_CLOSE,
38     TOK_DOT,
39     TOK_SEMICOLON,
40     TOK_COLON,
41     TOK_NEW,
42     TOK_NumericInt,
43     TOK_NumericFloat,
44     TOK_StringLiteral,
45     TOK_REGULAR_EXPRESSION_LITERAL,
46     TOK_THIS,
47     TOK_PLUSPLUS,
48     TOK_MINUSMINUS,
49     TOK_DELETE,
50     TOK_VOID,
51     TOK_TYPEOF,
52     TOK_MINUS,
53     TOK_TILDE,
54     TOK_EXCLAMATION,
55     TOK_MULTIPLY,
56     TOK_DIVIDE,
57     TOK_PERCENT,
58     TOK_PLUS,
59     TOK_SHIFT_LEFT,
60     TOK_SHIFT_RIGHT,
61     TOK_DOUBLESHIFT_RIGHT,
62     TOK_LESS,
63     TOK_GREATER,
64     TOK_LESSEQUAL,
65     TOK_GREATEREQUAL,
66     TOK_INSTANCEOF,
67     TOK_IN,
68     TOK_EQUAL_EQUAL,
69     TOK_NOT_EQUAL,
70     TOK_TRIPLE_EQUAL,
71     TOK_NOT_DOUBLEEQUAL,
72     TOK_AND,
73     TOK_XOR,
74     TOK_OR,
75     TOK_AND_AND,
76     TOK_OR_OR,
77     TOK_QUESTIONMARK,
78     TOK_EQUAL,
79     TOK_ASSIGNMENT_OPERATOR_NOEQUAL,
80     TOK_VAR,
81     TOK_IF,
82     TOK_ELSE,
83     TOK_DO,
84     TOK_WHILE,
85     TOK_FOR,
86     TOK_CONTINUE,
87     TOK_BREAK,
88     TOK_RETURN,
89     TOK_WITH,
90     TOK_SWITCH,
91     TOK_CASE,
92     TOK_DEFAULT,
93     TOK_THROW,
94     TOK_TRY,
95     TOK_CATCH,
96     TOK_FINALLY,
97     TOK_FUNCTION,
98     TOK_UNNORM_IDENTIFIER
99 };
100 
101 enum val_type {
102     vtype_undefined,
103     vtype_cstring,
104     vtype_string,
105     vtype_scope,
106     vtype_dval,
107     vtype_ival
108 };
109 
110 typedef struct token {
111     union {
112         const char *cstring;
113         char *string;
114         struct scope *scope; /* for function */
115         double dval;
116         long ival;
117     } val;
118     enum token_type type;
119     enum val_type vtype;
120 } yystype;
121 
122 /* inline functions to access the structure to ensure type safety */
123 
124 #define TOKEN_SET(DST, VTYPE, VAL)        \
125     do {                                  \
126         (DST)->vtype     = vtype_##VTYPE; \
127         (DST)->val.VTYPE = (VAL);         \
128     } while (0);
129 
130 #define cstring_invalid NULL
131 #define string_invalid NULL
132 #define scope_invalid NULL
133 /* there isn't really an invalid double, or long value, but we don't care
134  * about those values anyway, so -1 will be fine here */
135 #define dval_invalid -1
136 #define ival_invalid -1
137 
138 /* compatible if same type, or if we request a const char* instead of char*,
139  * but not viceversa! */
vtype_compatible(enum val_type orig,enum val_type req)140 static int vtype_compatible(enum val_type orig, enum val_type req)
141 {
142     return orig == req || (orig == vtype_string && req == vtype_cstring);
143 }
144 
145 #define TOKEN_GET(SRC, VTYPE) (vtype_compatible((SRC)->vtype, vtype_##VTYPE) ? (SRC)->val.VTYPE : VTYPE##_invalid)
146 
147 #define YYSTYPE yystype
148 #endif
149