-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathparser.ts
149 lines (118 loc) · 3.59 KB
/
parser.ts
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
import { CharStream, CommonTokenStream } from 'antlr4'
import SolidityLexer from './antlr/SolidityLexer'
import SolidityParser from './antlr/SolidityParser'
import {
ASTNode,
astNodeTypes,
ASTNodeTypeString,
ASTVisitor,
SourceUnit,
} from './ast-types'
import { ASTBuilder } from './ASTBuilder'
import ErrorListener from './ErrorListener'
import { buildCommentList, buildTokenList } from './tokens'
import { ParseOptions, Token, TokenizeOptions } from './types'
interface ParserErrorItem {
message: string
line: number
column: number
}
type ParseResult = SourceUnit & {
errors?: any[]
tokens?: Token[]
}
export class ParserError extends Error {
public errors: ParserErrorItem[]
constructor(args: { errors: ParserErrorItem[] }) {
super()
const { message, line, column } = args.errors[0]
this.message = `${message} (${line}:${column})`
this.errors = args.errors
if (Error.captureStackTrace !== undefined) {
Error.captureStackTrace(this, this.constructor)
} else {
this.stack = new Error().stack
}
}
}
export function tokenize(input: string, options: TokenizeOptions = {}): any {
const inputStream = new CharStream(input)
const lexer = new SolidityLexer(inputStream)
return buildTokenList(lexer.getAllTokens(), options)
}
export function parse(input: string, options: ParseOptions = {}): ParseResult {
const inputStream = new CharStream(input)
const lexer = new SolidityLexer(inputStream)
const tokenStream = new CommonTokenStream(lexer)
const parser = new SolidityParser(tokenStream)
const listener = new ErrorListener()
lexer.removeErrorListeners()
lexer.addErrorListener(listener)
parser.removeErrorListeners()
parser.addErrorListener(listener)
parser.buildParseTrees = true
const sourceUnit = parser.sourceUnit()
const astBuilder = new ASTBuilder(options)
astBuilder.visit(sourceUnit)
const ast: ParseResult | null = astBuilder.result
if (ast === null) {
throw new Error('ast should never be null')
}
if (options.tokens === true) {
ast.tokens = buildTokenList(tokenStream.tokens, options)
}
if (options.comments === true) {
ast.comments = buildCommentList(
tokenStream.tokens,
lexer.channelNames.indexOf('HIDDEN'),
options
)
}
if (listener.hasErrors()) {
if (options.tolerant !== true) {
throw new ParserError({ errors: listener.getErrors() })
}
ast.errors = listener.getErrors()
}
return ast
}
function _isASTNode(node: unknown): node is ASTNode {
if (typeof node !== 'object' || node === null) {
return false
}
const nodeAsASTNode = node as ASTNode
if (
Object.prototype.hasOwnProperty.call(nodeAsASTNode, 'type') &&
typeof nodeAsASTNode.type === 'string'
) {
return astNodeTypes.includes(nodeAsASTNode.type)
}
return false
}
export function visit(
node: unknown,
visitor: ASTVisitor,
nodeParent?: ASTNode
): void {
if (Array.isArray(node)) {
node.forEach((child) => visit(child, visitor, nodeParent))
}
if (!_isASTNode(node)) return
let cont = true
if (visitor[node.type] !== undefined) {
// TODO can we avoid this `as never`
cont = visitor[node.type]!(node as never, nodeParent)
}
if (cont === false) return
for (const prop in node) {
if (Object.prototype.hasOwnProperty.call(node, prop)) {
// TODO can we avoid this `as any`
visit((node as any)[prop], visitor, node)
}
}
const selector = (node.type + ':exit') as `${ASTNodeTypeString}:exit`
if (visitor[selector] !== undefined) {
// TODO can we avoid this `as never`
visitor[selector]!(node as never, nodeParent)
}
}