1 | const Program = require('./fs-parser/nodes/Program')
|
2 | const ParserContext = require('./ParserContext')
|
3 | const Parser = require('./Parser')
|
4 | const parserConf = require('./fs-parser/parserConf')
|
5 |
|
6 | class FirescriptParser {
|
7 | constructor (opts) {
|
8 |
|
9 | opts = opts || {}
|
10 |
|
11 |
|
12 |
|
13 | this.indentionSize = opts.indention || 2
|
14 | this.filename = opts.filename || null
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
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 |
|
55 | module.exports = FirescriptParser
|