Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const allTokens = [
WhiteSpace,
NumberLiteral,
StringLiteral,
LCurly,
RCurly,
LSquare,
RSquare,
Comma,
Colon,
True,
False,
Null
]
const JsonLexer = new Lexer(allTokens)
// ----------------- parser -----------------
class JsonParser extends CstParser {
// Unfortunately no support for class fields with initializer in ES2015, only in esNext...
// so the parsing rules are defined inside the constructor, as each parsing rule must be initialized by
// invoking RULE(...)
// see: https://github.com/jeffmo/es-class-fields-and-static-properties
constructor() {
super(allTokens)
// not mandatory, using $ (or any other sign) to reduce verbosity (this. this. this. this. .......)
const $ = this
// the parsing methods
$.RULE("json", () => {
As,
Or,
And,
// The Identifier must appear after the keywords because all keywords are valid identifiers.
Identifier,
Star,
Integer,
StringToken,
GreaterOrEqualThan,
GreaterThan,
LessOrEqualThan,
LessThan,
Equal,
} as const
const SelectLexer = new Lexer(Object.values(tokenVocabulary))
export const lex = (inputText: string) => {
const lexingResult = SelectLexer.tokenize(inputText)
if (lexingResult.errors.length > 0) {
throw Error(JSON.stringify(lexingResult.errors))
}
return lexingResult
}
group: Lexer.SKIPPED
})
const allTokens = [
WhiteSpace,
Number,
Element,
Default,
Dot,
Colon,
Equals,
SemiColon,
Ident
]
const backtrackingLexer = new Lexer(allTokens)
class BackTrackingParser extends CstParser {
constructor() {
super(allTokens)
const $ = this
this.RULE("statement", () => {
$.OR([
// both statements have the same prefix which may be of "infinite" length, this means there is no K for which
// we can build an LL(K) parser that can distinguish the two alternatives.
{
GATE: $.BACKTRACK($.withEqualsStatement),
ALT: () => {
$.SUBRULE($.withEqualsStatement)
}
const Charlie = createToken({ name: "Charlie", pattern: /C/ })
const WhiteSpace = createToken({
name: "WhiteSpace",
pattern: /\s+/,
group: Lexer.SKIPPED
})
const allTokens = [
WhiteSpace, // whitespace is normally very common so it should be placed first to speed up the lexer's performance
Alpha,
Bravo,
Charlie
]
const PhoneticLexer = new Lexer(allTokens)
// ----------------- parser -----------------
class MultiStartParser extends CstParser {
constructor() {
super(allTokens)
const $ = this
$.RULE("firstRule", () => {
$.CONSUME(Alpha)
$.OPTION(() => {
$.SUBRULE($.secondRule)
})
})
group: Lexer.SKIPPED,
line_breaks: true
})
const allTokens = [
WhiteSpace,
Select,
From,
Where,
Comma,
Identifier,
Integer,
GreaterThan,
LessThan
]
const SelectLexer = new Lexer(allTokens)
// ----------------- parser -----------------
class SelectParser extends Parser {
constructor(input) {
super(input, allTokens, { recoveryEnabled: true })
const $ = this
$.RULE("selectStatement", () => {
$.SUBRULE($.selectClause)
$.SUBRULE($.fromClause)
$.OPTION(() => {
$.SUBRULE($.whereClause)
})
})
const allTokens = [
WhiteSpace,
NumberLiteral,
StringLiteral,
LCurly,
RCurly,
LSquare,
RSquare,
Comma,
Colon,
True,
False,
Null
]
const JsonLexer = new Lexer(allTokens)
class JsonParserTypeScript extends CstParser {
constructor() {
super(allTokens)
this.performSelfAnalysis()
}
// In TypeScript the parsing rules are explicitly defined as class instance properties
// This allows for using access control (public/private/protected) and more importantly "informs" the TypeScript compiler
// about the API of our Parser, so referencing an invalid rule name (this.SUBRULE(this.oopsType);)
// is now a TypeScript compilation error.
public json = this.RULE("json", () => {
this.OR([
// using ES6 Arrow functions to reduce verbosity.
{ ALT: () => this.SUBRULE(this.object) },
{ ALT: () => this.SUBRULE(this.array) }
WhiteSpace,
RelationWord,
Und,
Vor,
Nach,
Kochen,
Wurstchen,
Wurst,
Raum,
Auf,
Den
]
// We can define a different Lexer for each of the sub grammars.
const EnglishLexer = new Lexer(englishTokens)
const GermanLexer = new Lexer(germanTokens)
// ----------------- parser -----------------
// Extending the base chevrotain CstParser class
class AbstractCommandsParser extends CstParser {
constructor(tokenVocabulary) {
// combining the token vocabularies of parent and child.
super(abstractTokens.concat(tokenVocabulary))
const $ = this
$.RULE("commands", () => {
$.SUBRULE($.command)
$.MANY(() => {
$.CONSUME(RelationWord)
const Plus = createToken({ name: "Plus", pattern: "+" })
const GreaterThan = createToken({ name: "GreaterThan", pattern: ">" })
const Tilde = createToken({ name: "Tilde", pattern: "~" })
const Hash = createToken({
name: "Hash",
pattern: MAKE_PATTERN("#{{name}}")
})
const Dot = createToken({ name: "Dot", pattern: "." })
const Comma = createToken({ name: "Comma", pattern: "," })
const Colon = createToken({ name: "Colon", pattern: ":" })
const LCurly = createToken({ name: "LCurly", pattern: "{" })
const RCurly = createToken({ name: "RCurly", pattern: "}" })
ImportSym.LONGER_ALT = VariableName
const LessLexer = new Lexer(lessTokens)
// ----------------- parser -----------------
class LessParser extends Parser {
constructor(input) {
super(input, lessTokens, {
ignoredIssues: {
selector: { OR: true }
}
})
const $ = this
$.RULE("primary", () => {
$.MANY(() => {
$.OR2([
EndComment,
Whitespace,
StartOfLine,
EndOfLine,
Beginning,
Carat,
Modifier,
Only,
Not,
Identifier,
Period,
OpenParens,
CloseParens,
];
const lexer = new Lexer(allTokens);
class GrammarParser extends Parser {
constructor(input) {
super(input, allTokens);
const $ = this;
let openTokenPos = -1;
this.root = $.RULE('assertion', () => {
$.OPTION(() => $.CONSUME(Whitespace));
const commentToken = $.CONSUME(Comment);
openTokenPos = commentToken.startColumn;
$.OPTION2(() => $.CONSUME2(Whitespace));
const positions = $.SUBRULE($.positions);
$.CONSUME3(Whitespace);
const scopes = $.SUBRULE($.rules);
$.OPTION3(() => $.CONSUME4(Whitespace));
SEMICOLON,
SHIFT_OPERATOR,
STEP,
STOP,
STRING,
STRING_LITERAL,
SUB,
TERMINATOR,
THEN,
TO,
TYPE_DECLARATION,
UNARY,
WHILE
} from './Tokens'
const BRSLexer = new Lexer(ALL_TOKENS, {
deferDefinitionErrorsHandling: true,
ensureOptimizations: true,
positionTracking: 'onlyStart'
})
const operator = { LABEL: 'operator' }
const right = { LABEL: 'right' }
const left = { LABEL: 'left' }
const body = { LABEL: 'body' }
const Declaration = { LABEL: 'Declaration' }
const Empty = { LABEL: 'Empty' }
const Statement = { LABEL: 'Statement' }
export class RokuBRSParser extends Parser {
public Program = this.RULE('Program', () => {