33{count, starts, compact, last, repeat,
44locationDataToString, throwSyntaxError} = require ' ./helpers'
55
6- # ast node builders
7- astLeafNode = (type , value = null ) -> {type, value}
8- astBranchNode = (type , value = null , children = []) -> {type, value, children}
6+ # parse tree node builders
7+ parseTreeLeafNode = (type , value = null ) -> {type, value}
8+ parseTreeBranchNode = (type , value = null , children = []) -> {type, value, children}
99
1010exports .transform = (code , opts ) ->
1111 serialise new Parser ().parse code, opts
1212
1313exports .Parser = Parser = class Parser
1414 parse : (code , opts = {}) ->
15- @ast = astBranchNode ROOT # abstract syntax tree
16- @activeStates = [@ast ] # stack tracking current ast position (initialised with root node )
15+ @parseTree = parseTreeBranchNode ROOT # concrete syntax tree (initialised with root node)
16+ @activeStates = [@parseTree ] # stack tracking current parse tree position (starting with root)
1717 @chunkLine = 0 # The start line for the current @chunk.
1818 @chunkColumn = 0 # The start column of the current @chunk.
1919 code = @ clean code # The stripped, cleaned original source code.
@@ -41,12 +41,12 @@ exports.Parser = Parser = class Parser
4141
4242 i += consumed
4343
44- unless @ activeBranchNode () == @ast
44+ unless @ activeBranchNode () == @parseTree
4545 throwSyntaxError \
4646 " Unexpected EOF: unclosed #{ @ activeBranchNode ().type } " ,
4747 first_line : @chunkLine , first_column : @chunkColumn
4848
49- @ast # return completed ast
49+ @parseTree # return completed parseTree
5050
5151 # lex/parse states
5252
@@ -55,7 +55,7 @@ exports.Parser = Parser = class Parser
5555 return 0 unless match = @chunk .match COMMENT
5656 [comment , here ] = match
5757
58- @ addLeafNodeToActiveBranch astLeafNode CS_COMMENT, comment
58+ @ addLeafNodeToActiveBranch parseTreeLeafNode CS_COMMENT, comment
5959
6060 comment .length
6161
@@ -64,7 +64,7 @@ exports.Parser = Parser = class Parser
6464 return 0 unless match = HEREDOC .exec @chunk
6565 heredoc = match[0 ]
6666
67- @ addLeafNodeToActiveBranch astLeafNode CS_HEREDOC, heredoc
67+ @ addLeafNodeToActiveBranch parseTreeLeafNode CS_HEREDOC, heredoc
6868
6969 heredoc .length
7070
@@ -76,7 +76,7 @@ exports.Parser = Parser = class Parser
7676 when ' "' then string = @ balancedString @chunk , ' "'
7777 return 0 unless string
7878
79- @ addLeafNodeToActiveBranch astLeafNode CS_STRING, string
79+ @ addLeafNodeToActiveBranch parseTreeLeafNode CS_STRING, string
8080
8181 string .length
8282
@@ -85,7 +85,7 @@ exports.Parser = Parser = class Parser
8585 return 0 unless @chunk .charAt (0 ) is ' `' and match = JSTOKEN .exec @chunk
8686 script = match[0 ]
8787
88- @ addLeafNodeToActiveBranch astLeafNode JS_ESC, script
88+ @ addLeafNodeToActiveBranch parseTreeLeafNode JS_ESC, script
8989
9090 script .length
9191
@@ -95,29 +95,24 @@ exports.Parser = Parser = class Parser
9595
9696 return 0 unless selfClosing or @chunk .indexOf (" </#{ tagName} >" , input .length ) > - 1
9797
98- @ pushActiveBranchNode astBranchNode CSX_EL, tagName
99- @ addLeafNodeToActiveBranch @ rewriteAttributes attributesText
100-
101- # console.log CSX_START
98+ @ pushActiveBranchNode parseTreeBranchNode CSX_EL, tagName
99+ @ addLeafNodeToActiveBranch @ parseCSXAttributes attributesText
102100
103101 if selfClosing
104102 @ popActiveBranchNode () # close csx tag
105- # console.log CSX_END
106103
107104 input .length
108105
109106 csxEscape : ->
110107 return 0 unless @ activeBranchNode ().type == CSX_EL and @chunk .charAt (0 ) == ' {'
111108
112- @ pushActiveBranchNode astBranchNode CSX_ESC
113- # console.log CSX_ESC_START
109+ @ pushActiveBranchNode parseTreeBranchNode CSX_ESC
114110 return 1
115111
116112 csxUnescape : ->
117113 return 0 unless @ activeBranchNode ().type == CSX_ESC and @chunk .charAt (0 ) == ' }'
118114
119115 @ popActiveBranchNode () # close csx escape
120- # console.log CSX_ESC_END
121116 return 1
122117
123118 csxEnd : ->
@@ -131,15 +126,14 @@ exports.Parser = Parser = class Parser
131126 first_line : @chunkLine , first_column : @chunkColumn
132127
133128 @ popActiveBranchNode () # close csx tag
134- # console.log CSX_END
135129
136130 input .length
137131
138132 csxText : ->
139133 return 0 unless @ activeBranchNode ().type == CSX_EL
140134
141135 unless @ newestNode ().type == CSX_TEXT
142- @ addLeafNodeToActiveBranch astLeafNode CSX_TEXT, ' ' # init value as string
136+ @ addLeafNodeToActiveBranch parseTreeLeafNode CSX_TEXT, ' ' # init value as string
143137
144138 # newestNode is (now) CSX_TEXT
145139 @ newestNode ().value += @chunk .charAt 0
@@ -151,14 +145,14 @@ exports.Parser = Parser = class Parser
151145 # return 0 unless @activeBranchNode().type == ROOT or @activeBranchNode().type == CSX_ESC
152146
153147 unless @ newestNode ().type == CS
154- @ addLeafNodeToActiveBranch astLeafNode CS, ' ' # init value as string
148+ @ addLeafNodeToActiveBranch parseTreeLeafNode CS, ' ' # init value as string
155149
156150 # newestNode is (now) CS
157151 @ newestNode ().value += @chunk .charAt 0
158152
159153 return 1
160154
161- # ast helpers
155+ # parseTree helpers
162156
163157 activeBranchNode : -> last (@activeStates )
164158
@@ -173,8 +167,8 @@ exports.Parser = Parser = class Parser
173167 addLeafNodeToActiveBranch : (node ) ->
174168 @ activeBranchNode ().children .push (node)
175169
176- rewriteAttributes : (attributesText ) ->
177- astBranchNode CSX_ATTRIBUTES, null , do ->
170+ parseCSXAttributes : (attributesText ) ->
171+ parseTreeBranchNode CSX_ATTRIBUTES, null , do ->
178172 while attrMatches = TAG_ATTRIBUTES .exec attributesText
179173 [ attrNameValText, attrName, doubleQuotedVal,
180174 singleQuotedVal, csEscVal, bareVal ] = attrMatches
@@ -184,29 +178,29 @@ exports.Parser = Parser = class Parser
184178 first_line : @chunkLine , first_column : @chunkColumn
185179
186180 if doubleQuotedVal # "value"
187- astBranchNode (CSX_ATTR_PAIR, null , [
188- astLeafNode (CSX_ATTR_KEY, " \" #{ attrName} \" " )
189- astLeafNode (CSX_ATTR_VAL, " \" #{ doubleQuotedVal} \" " )
181+ parseTreeBranchNode (CSX_ATTR_PAIR, null , [
182+ parseTreeLeafNode (CSX_ATTR_KEY, " \" #{ attrName} \" " )
183+ parseTreeLeafNode (CSX_ATTR_VAL, " \" #{ doubleQuotedVal} \" " )
190184 ])
191185 else if singleQuotedVal # 'value'
192- astBranchNode (CSX_ATTR_PAIR, null , [
193- astLeafNode (CSX_ATTR_KEY, " \" #{ attrName} \" " )
194- astLeafNode (CSX_ATTR_VAL, " '#{ singleQuotedVal} '" )
186+ parseTreeBranchNode (CSX_ATTR_PAIR, null , [
187+ parseTreeLeafNode (CSX_ATTR_KEY, " \" #{ attrName} \" " )
188+ parseTreeLeafNode (CSX_ATTR_VAL, " '#{ singleQuotedVal} '" )
195189 ])
196190 else if csEscVal # {value}
197- astBranchNode (CSX_ATTR_PAIR, null , [
198- astLeafNode (CSX_ATTR_KEY, " \" #{ attrName} \" " )
199- astBranchNode (CSX_ESC, null , [astLeafNode (CS, csEscVal)])
191+ parseTreeBranchNode (CSX_ATTR_PAIR, null , [
192+ parseTreeLeafNode (CSX_ATTR_KEY, " \" #{ attrName} \" " )
193+ parseTreeBranchNode (CSX_ESC, null , [parseTreeLeafNode (CS, csEscVal)])
200194 ])
201195 else if bareVal # value
202- astBranchNode (CSX_ATTR_PAIR, null , [
203- astLeafNode (CSX_ATTR_KEY, " \" #{ attrName} \" " )
204- astLeafNode (CSX_ATTR_VAL, " \" #{ bareVal} \" " )
196+ parseTreeBranchNode (CSX_ATTR_PAIR, null , [
197+ parseTreeLeafNode (CSX_ATTR_KEY, " \" #{ attrName} \" " )
198+ parseTreeLeafNode (CSX_ATTR_VAL, " \" #{ bareVal} \" " )
205199 ])
206200 else
207- astBranchNode (CSX_ATTR_PAIR, null , [
208- astLeafNode (CSX_ATTR_KEY, " \" #{ attrName} \" " )
209- astLeafNode (CSX_ATTR_VAL, ' true' )
201+ parseTreeBranchNode (CSX_ATTR_PAIR, null , [
202+ parseTreeLeafNode (CSX_ATTR_KEY, " \" #{ attrName} \" " )
203+ parseTreeLeafNode (CSX_ATTR_VAL, ' true' )
210204 ])
211205
212206 # helpers (from cs lexer)
@@ -277,7 +271,7 @@ exports.Parser = Parser = class Parser
277271 @ error " missing #{ stack .pop () } , starting"
278272
279273
280- exports .serialise = serialise = (ast ) ->
274+ exports .serialise = serialise = (parseTree ) ->
281275 serialiseNode = (node ) -> serialisers[node .type ](node)
282276
283277 genericBranchSerialiser = (node ) ->
@@ -331,7 +325,7 @@ exports.serialise = serialise = (ast) ->
331325 CSX_ATTR_KEY : genericLeafSerialiser
332326 CSX_ATTR_VAL : genericLeafSerialiser
333327
334- serialiseNode (ast )
328+ serialiseNode (parseTree )
335329
336330
337331# Constants
0 commit comments