/
parseEToSExpression.emaker
165 lines (139 loc) · 4.55 KB
/
parseEToSExpression.emaker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Copyright 2005-2007 Kevin Reid, under the terms of the MIT X license
# found at http://www.opensource.org/licenses/mit-license.html ................
pragma.enable("easy-return")
pragma.disable("explicit-result-guard")
pragma.enable("dot-props")
pragma.enable("accumulator")
def makeConvertENode2Term := <elang:visitors.makeConvertENode2Term>
def makeStringReader := <import:java.io.makeStringReader>
def makeTextWriter := <elib:oldeio.makeTextWriter>
def makeELexer := <elang:syntax.makeELexer>
def convert := makeConvertENode2Term()
def clQuote(s :String) {
return "\"" + s.replaceAll("\\", "\\\\").replaceAll("\"", "\\\"") + "\""
}
def termToSExpression(term) {
if (term =~ term`.String.@_`) {
# Quote in accordance with Common Lisp reader syntax - no \letter escapes.
return clQuote(term.getOptData())
} else if (term =~ term`.int.@_`) {
return term.asText()
} else if (term =~ term`.char.@_`) {
return "#\\" + E.toString(term.getOptData())
} else if (term =~ term`.float64.@_`) {
# Print as double-float
def floatString := term.asText()
if (floatString.startOf("e") != -1) {
return floatString.replaceAll("e", "d")
} else {
return floatString + "d0"
}
} else {
return "(|" + term::tag::tagName + "|" + accum "" for x in term::args { _ + " " + termToSExpression(x) } + ")"
}
}
# --- ---
def author(<unsafe>) {
#def <eantlr> := <unsafe:org.erights.e.elang.syntax.antlr.*>
def <eantlr> := <unsafe>
def <antlr> := <unsafe:antlr.*>
def makeEParser := <eantlr:makeEParser>
def makeEALexer := <eantlr:makeEALexer>
def makeCountingLexerSharedInputState := <eantlr:makeCountingLexerSharedInputState>
def makeQuasiLexer := <eantlr:makeQuasiLexer>
def makeTokenMultiBuffer := <antlr:makeTokenMultiBuffer>
def makeTokenStreamSelector := <antlr:makeTokenStreamSelector>
def makeDumpASTVisitor := <antlr:makeDumpASTVisitor>
def makeCommonToken := <antlr:makeCommonToken>
def tokenSymbols := accum [] for tn in makeEParser.get_tokenNames() { _.with("e.grammar::|" + tn.replaceAll("\\", "\\\\").replaceAll("|", "\\|") + "|") }
def antlrParse(fname, text, isPattern, quasiInfo, props) {
def elexer := makeEALexer(makeCountingLexerSharedInputState(makeStringReader(text), 0))
def qlexer := makeQuasiLexer(elexer.getInputState())
def tb := makeTokenMultiBuffer(["e", "quasi"], [elexer, qlexer])
for lexer in [elexer, qlexer] {
lexer.setSelector(tb)
lexer.setFilename(fname)
lexer.setTokenObjectClass("ExtToken")
}
def parser := makeEParser(tb)
parser.setFilename(fname)
parser.setASTNodeClass("ExtAST")
switch (quasiInfo) {
match [vh, ph] { elexer.setHoles(vh, ph) }
match ==null {}
}
parser.setSyntax(makeCommonToken(-1, "0.9"))
if (props != null) {
for k => v in props {
parser.setPocket(makeCommonToken(-1, k), v)
}
}
if (isPattern) {
parser.pattern()
} else {
parser.start()
}
def ast := parser.getAST()
def [out, sb] := makeTextWriter.makeBufferingPair()
def parentheses(body) {
out.print("(")
body()
out.print(")")
}
def printChain(var ast) {
while (ast != null) {
out.print(" ")
parentheses(fn{
parentheses(fn{
out.print(tokenSymbols[ast.getType()], " ", ast.getLine(), " ", ast.getColumn(), " ", clQuote(ast.getText()))
})
printChain(ast.getFirstChild())
})
ast := ast.getNextSibling()
}
}
parentheses(fn{
parentheses(fn{
printChain(ast)
})
out.print("#.")
parentheses(fn{
out.print("e.elib:e. e.elib:+the-make-const-map+ :|fromPairs| '#")
parentheses(fn{
for k => v in parser.getPockets() {
out.print("#")
parentheses(fn{
out.print(clQuote(k))
out.print(clQuote(v))
})
}
})
})
})
return sb.snapshot()
}
def parseEToSExpression(verb, args) {
return try {
switch (verb) {
match =="antlrParse" {
E.call(antlrParse, "run", args)
}
match =="run" {
def [source] := args
termToSExpression(convert(e__quasiParser(source)))
}
match =="isIdentifier" {
def [word] := args
if (makeELexer.isIdentifier(word)) {
"cl:t "
} else {
"cl:nil "
}
}
}
} catch p {
`(ERROR ${clQuote(E.toQuote(p))})`
}
}
return parseEToSExpression
}