Skip to content

Commit 09a79fc

Browse files
committed
renamed 'ast' stuff to 'parseTree'... probably a more accurate description
1 parent 89914dc commit 09a79fc

File tree

4 files changed

+46
-53
lines changed

4 files changed

+46
-53
lines changed

Cakefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ log = (message, color, explanation) ->
1515

1616
# Build transformer from source.
1717
build = (cb) ->
18-
exec 'mkdir', ['-p','bin', 'lib'], ->
18+
run 'mkdir', ['-p','bin', 'lib'], ->
1919
compile ['transformer', 'helpers'], 'src/', 'lib/', ->
20-
exec 'cp', ['src/htmlelements.js','lib/htmlelements.js'], cb
20+
run 'cp', ['src/htmlelements.js','lib/htmlelements.js'], cb
2121

2222
compile = (srcFiles, srcDir, destDir, cb) ->
2323
srcFilePaths = srcFiles.map (filename) -> "#{srcDir}/#{filename}.coffee"
2424
args = ['--bare', '-o', destDir, '--compile'].concat srcFilePaths
2525
coffee args, cb
2626

2727
# Run CoffeeScript command
28-
coffee = (args, cb) -> exec 'coffee', args, cb
28+
coffee = (args, cb) -> run 'coffee', args, cb
2929

30-
exec = (executable, args = [], cb) ->
30+
run = (executable, args = [], cb) ->
3131
proc = spawn executable, args
3232
proc.stdout.on 'data', (buffer) -> log buffer.toString(), green
3333
proc.stderr.on 'data', (buffer) -> log buffer.toString(), red

examples/car.csx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ Car = React.createClass
1010
<p>Which seat can I take? {@props.seat}</p>
1111
</Car>
1212

13-
React.renderComponent \
14-
<Car seat="front, obvs" />,
13+
React.renderComponent <Car seat="front, obvs" />,
1514
document.getElementById 'container'

examples/demo.coffee

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fs = require 'fs'
77

88
start = new Date()
99

10-
ast = new Parser().parse(fs.readFileSync('./car.csx', 'utf8'))
10+
parseTree = new Parser().parse(fs.readFileSync('./car.csx', 'utf8'))
1111

1212
'''
1313
@@ -66,10 +66,10 @@ React.createClass
6666
6767
6868
"""
69-
console.log 'AST:'
70-
console.log JSON.stringify(ast, null, 4)
71-
console.log 'Transformed CSX:'
72-
coffeescriptCode = serialise ast
69+
console.log 'Parse tree:'
70+
console.log JSON.stringify(parseTree, null, 4)
71+
console.log 'Transformed to coffee:'
72+
coffeescriptCode = serialise parseTree
7373
console.log coffeescriptCode
7474
console.log 'Compiled to JS:'
7575
console.log(coffeeCompile(coffeescriptCode))

src/transformer.coffee

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
{count, starts, compact, last, repeat,
44
locationDataToString, 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

1010
exports.transform = (code, opts) ->
1111
serialise new Parser().parse code, opts
1212

1313
exports.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

Comments
 (0)