@maniascript/parser
Version:
Maniascript parser
76 lines (48 loc) • 1.87 kB
Markdown
Parse a Maniascript string and generate an [ANTLR](https://www.antlr.org) parse tree and an AST.
Install with npm: `npm install @maniascript/parser`.
`import { parse } from '@maniascript/parser'` or `const { parse } = require('@maniascript/parser')`
`parse` is a function that takes a Maniascript string as input, parses it and returns the result.
```ts
import { parse } from '@maniascript/parser'
const result = parse(`
main() {
declare Integer Test = 0;
}
`)
```
`result` will have the following properties after a successful parsing
```ts
{
success: true,
errors: [],
tree: { ... },
ast: { ... }
}
```
`result` will have the following properties after a failed parsing
```ts
{
success: false,
errors: [{ line: 1, column: 1, message: 'There is an error' }],
tree: { ... },
ast: { ... }
}
```
`import { ManiaScriptLexer } from '@maniascript/parser'` or `const { ManiaScriptLexer } = require('@maniascript/parser')`
`ManiaScriptLexer` is the ANTLR lexer class.
`import { ManiaScriptParser } from '@maniascript/parser'` or `const { ManiaScriptParser } = require('@maniascript/parser')`
`ManiaScriptParser` is the ANTLR parser class.
`import { ManiaScriptListener } from '@maniascript/parser'` or `const { ManiaScriptListener } = require('@maniascript/parser')`
`ManiaScriptListener` is the listenener generated by ANTLR to walk through the parse tree.
`import { ManiaScriptVisitor } from '@maniascript/parser'` or `const { ManiaScriptVisitor } = require('@maniascript/parser')`
`ManiaScriptVisitor` is the visitor generated by ANTLR to walk through the parse tree.
The documentation for the abstract syntax tree generated by the parser is available [here](doc/ast.md).