blob: 874b73545180e38fb5fea9acce05a670f612533e [file] [log] [blame]
Brian Silverman70325d62015-09-20 17:00:43 -04001# Copyright (c) 2008, Google Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14# * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29# ---
30#
31# Author: falmeida@google.com (Filipe Almeida)
32
33name = 'jsparser'
34
35comment = 'Simplified finite state machine for tracking of javascript states'
36
37condition('q', '\''),
38condition('dq', '\"'),
39condition('/', '/'),
40condition('*', '*'),
41condition('[', '['),
42condition(']', ']'),
43condition('lf', '\n'),
44condition('backslash', '\\'),
45condition('default', '[:default:]')
46
47# Main javascript body.
48state(name = 'js_text',
49 external = 'text',
50 transitions = [
51 ['q', 'js_q'],
52 ['dq', 'js_dq'],
53 ['/', 'js_slash'],
54 ['default', 'js_text']
55 ])
56
57# Single quoted string literal.
58state(name = 'js_q',
59 external = 'q',
60 transitions = [
61 ['backslash', 'js_q_e'],
62 ['q', 'js_text'],
63 ['default', 'js_q']
64 ])
65
66# Javascript escaped character in a single quoted string literal.
67state(name = 'js_q_e',
68 external = 'q',
69 transitions = [
70 ['default', 'js_q']
71 ])
72
73# Double quoted string literal
74state(name = 'js_dq',
75 external = 'dq',
76 transitions = [
77 ['backslash', 'js_dq_e'],
78 ['dq', 'js_text'],
79 ['default', 'js_dq']
80 ])
81
82# Javascript escaped character in a double quoted string literal.
83state(name = 'js_dq_e',
84 external = 'dq',
85 transitions = [
86 ['default', 'js_dq']
87 ])
88
89# Possible start of a javascript comment.
90state(name = 'js_slash',
91 external = 'text',
92 transitions = [
93 ['/', 'js_comment_ln'],
94 ['*', 'js_comment_ml'],
95 ['default', 'js_text']
96 ])
97
98# Possible start of a regular expression literal.
99#
100# The state diagram does not reach this state directly. When js_slash is
101# reached, the function enter_state_js_slash() is called, which checks if the
102# last token belongs to the set of tokens that can precede a regular
103# expression, in which case it changes the state to js_regexp_slash.
104#
105# For more information please read the comments in
106# jsparser.c:enter_state_js_slash().
107state(name = 'js_regexp_slash',
108 external = 'text',
109 transitions = [
110 ['/', 'js_comment_ln'],
111 ['*', 'js_comment_ml'],
112 ['backslash', 'js_regexp_e'],
113 ['[', 'js_regexp_bracket'],
114 ['default', 'js_regexp']
115 ])
116
117# Regular expression literal.
118state(name = 'js_regexp',
119 external = 'regexp',
120 transitions = [
121 ['backslash', 'js_regexp_e'],
122 ['[', 'js_regexp_bracket'],
123 ['/', 'js_text'],
124 ['default', 'js_regexp']
125 ])
126
127# Regexp bracket expression
128state(name = 'js_regexp_bracket',
129 external = 'regexp',
130 transitions = [
131 ['backslash', 'js_regexp_bracket_e'],
132 [']', 'js_regexp'],
133 ['default', 'js_regexp_bracket']
134 ])
135
136# Backslash escaped regexp bracket expression
137state(name = 'js_regexp_bracket_e',
138 external = 'regexp',
139 transitions = [
140 ['default', 'js_regexp_bracket']
141 ])
142
143# Escaped regular expression char.
144state(name = 'js_regexp_e',
145 external = 'regexp',
146 transitions = [
147 ['default', 'js_regexp']
148 ])
149
150# Start of a single line javascript comment (//).
151state(name = 'js_comment_ln',
152 external = 'comment',
153 transitions = [
154 ['lf', 'js_comment_after'],
155 ['default', 'js_comment_ln']
156 ])
157
158# Start of a multiline javascript comment (/*).
159state(name = 'js_comment_ml',
160 external = 'comment',
161 transitions = [
162 ['*', 'js_comment_ml_close'],
163 ['default', 'js_comment_ml']
164 ])
165
166# Close of a multiline javascript comment (*/).
167state(name = 'js_comment_ml_close',
168 external = 'comment',
169 transitions = [
170 ['/', 'js_comment_after'],
171 ['default', 'js_comment_ml']
172 ])
173
174# Ending character of a javascript comment.
175# In can either be a '/ in the case of a multiline comment, or a line
176# terminator in the case of a single line comment.
177# This is needed so we don't insert the '/' or the new line character into the
178# ring buffer.
179state(name = 'js_comment_after',
180 external = 'text',
181 transitions = [
182 ['q', 'js_q'],
183 ['dq', 'js_dq'],
184 ['/', 'js_slash'],
185 ['default', 'js_text']
186 ])