UNPKG

1.55 kBJavaScriptView Raw
1const Program = require('./fs-parser/nodes/Program')
2const ParserContext = require('./ParserContext')
3const Parser = require('./Parser')
4const parserConf = require('./fs-parser/parserConf')
5
6class FirescriptParser {
7 constructor (opts) {
8 // super(opts)
9 opts = opts || {}
10
11 // this.setLocation = conf.setLocation || false
12 // this.setRange = conf.setRange || false
13 this.indentionSize = opts.indention || 2
14 this.filename = opts.filename || null
15 // this.keyWords = 'import|func|class|const|let|var|return'
16 // this.literalPattern = '\'[^]+?\'|\\d+'
17 // this.binaryOperatorPattern = /^[+*/&-]$/
18 // this.showIndex = conf.index === undefined ? true : conf.index
19 // this.showLines = conf.lines === undefined ? true : conf.line
20 // this.callStack = []
21 // this.sourceType = conf.sourceType
22 }
23
24 parse (source) {
25 try {
26 parserConf.indentionSize = this.indentionSize
27 parserConf.filename = this.filename
28 const parser = new Parser(parserConf)
29 parser.parse(source)
30
31 const ast = new Program(parser, this.sourceType)
32 const ctx = new ParserContext({
33 setLocation: this.setLocation,
34 setRange: this.setRange
35 })
36
37 return ast.resolve(ctx)
38 } catch (err) {
39 if (!err.token) {
40 throw err
41 }
42
43 this.syntaxError(err)
44 }
45 }
46
47 tokenize (source) {
48 parserConf.indentionSize = this.indentionSize
49 const parser = new Parser(parserConf)
50 parser.parse(source)
51 return parser.tokenize()
52 }
53}
54
55module.exports = FirescriptParser