forked from solidity-parser/parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.ts
135 lines (105 loc) · 3.53 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
import { ANTLRInputStream, CommonTokenStream } from 'antlr4ts'
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 { 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 ANTLRInputStream(input)
const lexer = new SolidityLexer(inputStream)
return buildTokenList(lexer.getAllTokens(), options)
}
export function parse(
input: string,
options: ParseOptions = {}
): ParseResult {
const inputStream = new ANTLRInputStream(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.buildParseTree = true
const sourceUnit = parser.sourceUnit()
const astBuilder = new ASTBuilder(options)
astBuilder.visit(sourceUnit)
const ast: ParseResult | null = astBuilder.result as any
if (ast === null) {
throw new Error('ast should never be null')
}
let tokenList: Token[] = []
if (options.tokens === true) {
tokenList = buildTokenList(tokenStream.getTokens(), options)
}
if (options.tolerant !== true && listener.hasErrors()) {
throw new ParserError({ errors: listener.getErrors() })
}
if (options.tolerant === true && listener.hasErrors()) {
ast.errors = listener.getErrors()
}
if (options.tokens === true) {
ast.tokens = tokenList
}
return ast
}
function _isASTNode(node: unknown): node is ASTNode {
if (typeof node !== 'object' || node === null) {
return false
}
const nodeAsAny: any = node
if (Object.prototype.hasOwnProperty.call(nodeAsAny, 'type') && typeof nodeAsAny.type === "string") {
return astNodeTypes.includes(nodeAsAny.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 any`
cont = visitor[node.type]!(node as any, 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 any`
visitor[selector]!(node as any, nodeParent)
}
}