Last active
August 2, 2021 23:33
-
-
Save tecklund/193cd707ea5c2cb98940f7c5cfcaffc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grammar todo; | |
elements | |
: (element|emptyLine)* EOF | |
; | |
element | |
: '*' ( ' ' | '\t' )* CONTENT NL+ | |
; | |
emptyLine | |
: NL | |
; | |
NL | |
: '\r' | '\n' | |
; | |
CONTENT | |
: [a-zA-Z0-9_][a-zA-Z0-9_ \t]* | |
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { CharStreams, CommonTokenStream } from 'antlr4ts'; | |
import { todoLexer } from './todoLexer'; | |
import { todoListener } from './todoListener'; | |
import { ElementContext, todoParser } from './todoParser'; | |
import { ParseTreeWalker } from 'antlr4ts/tree/ParseTreeWalker' | |
// Create the lexer and parser | |
let inputStream = CharStreams.fromString("* hello\n* there\n\n\n") | |
let lexer = new todoLexer(inputStream); | |
let tokenStream = new CommonTokenStream(lexer); | |
let parser = new todoParser(tokenStream); | |
// Parse the input, where `compilationUnit` is whatever entry point you defined | |
let tree = parser.elements(); | |
class EnterFunctionListener implements todoListener { | |
// Assuming a parser rule with name: `functionDeclaration` | |
enterElement(context: ElementContext) { | |
console.log(`${context.CONTENT()}`) | |
} | |
// other enterX functions... | |
} | |
// // Create the listener | |
const listener: todoListener = new EnterFunctionListener(); | |
// // Use the entry point for listeners | |
ParseTreeWalker.DEFAULT.walk(listener, tree) | |
// OUTPUT: | |
// hello | |
// there |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment