Skip to content

Instantly share code, notes, and snippets.

@tecklund
Last active August 2, 2021 23:33
Show Gist options
  • Save tecklund/193cd707ea5c2cb98940f7c5cfcaffc3 to your computer and use it in GitHub Desktop.
Save tecklund/193cd707ea5c2cb98940f7c5cfcaffc3 to your computer and use it in GitHub Desktop.
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]*
;
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