pegisland
Version:
General PEG-based parser supporting island grammars with lake symbols
117 lines • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PegParser = void 0;
// Copyright (C) 2021- Katsumi Okuda. All rights reserved.
const InitialPegBuilder_1 = require("./InitialPegBuilder");
const PackratParser_1 = require("./PackratParser");
const grammar = {
grammar: ['', 'Spacing', ['+', 'Definition']],
OptAnnotations: ['*', ['terminal', /@water\s*/]],
Definition: [
'',
'OptAnnotations',
'Identifier',
'LEFT_ARROW',
'Expression',
['?', 'SEMICOLON'],
],
Expression: ['', 'Rewriting', ['*', ['', 'SLASH', 'Rewriting']]],
Rewriting: ['', 'Sequence', ['?', ['', 'RIGHT_ARROW', 'String']]],
Sequence: ['*', ['', 'Prefix', ['!', 'LEFT_ARROW']]],
Prefix: ['', ['?', ['/', 'AND', 'NOT']], 'Suffix'],
Suffix: [
'',
'Primary',
[
'?',
[
'/',
['', 'STAR_PLUS', 'Primary'],
['', 'PLUS_PLUS', 'Primary'],
['', 'COLON_NOT', 'Primary'],
['', 'COLON', 'Primary'],
'QUESTION',
'STAR',
'PLUS',
],
],
],
Primary: [
'/',
'Regexp',
['', 'LAKE_OPEN', 'Expression', 'LAKE_CLOSE'],
'NamedIdentifier',
['', 'OPEN', 'Expression', 'CLOSE'],
'String',
'Class',
'DOT',
],
NamedIdentifier: [
'',
[
'?',
[
'',
['terminal', /[a-zA-Z][a-zA-Z0-9_]*|<[a-zA-Z][a-zA-Z0-9_]*>/],
['terminal', /@/],
],
],
'Identifier',
],
Identifier: [
'',
['terminal', /[a-zA-Z][a-zA-Z0-9_]*|<[a-zA-Z][a-zA-Z0-9_]*>/],
'Spacing',
],
String: [
'',
['/', ['terminal', /"(\\.|[^"])+"/], ['terminal', /'(\\.|[^'])+'/]],
'Spacing',
],
Regexp: [
'',
['/', ['terminal', /r"(\\.|[^"])+"/], ['terminal', /r'(\\.|[^'])+'/]],
'Spacing',
],
Class: ['', ['terminal', /\^?\[[^\]]+\]/], 'Spacing'],
Char: [
'/',
['terminal', /\\[nrt'"[\]\\]/],
['terminal', /\\[0-2][0-7][0-7]/],
['terminal', /\\[0-7][0-7]?/],
['terminal', /[^\\]/],
],
LEFT_ARROW: ['', ['terminal', /[=]|<-/], 'Spacing'],
RIGHT_ARROW: ['', ['terminal', /->/], 'Spacing'],
SEMICOLON: ['', ['terminal', /;/], 'Spacing'],
SLASH: ['', ['terminal', /\//], 'Spacing'],
AND: ['', ['terminal', /&/], 'Spacing'],
NOT: ['', ['terminal', /!/], 'Spacing'],
QUESTION: ['', ['terminal', /\?/], 'Spacing'],
STAR_PLUS: ['', ['terminal', /\*\+/], 'Spacing'],
PLUS_PLUS: ['', ['terminal', /\+\+/], 'Spacing'],
STAR: ['', ['terminal', /\*/], 'Spacing'],
PLUS: ['', ['terminal', /\+/], 'Spacing'],
OPEN: ['', ['terminal', /\(/], 'Spacing'],
CLOSE: ['', ['terminal', /\)/], 'Spacing'],
LAKE_OPEN: ['', ['terminal', /<</], 'Spacing'],
LAKE_CLOSE: ['', ['terminal', />>/], 'Spacing'],
DOT: ['', ['terminal', /\.|_/], 'Spacing'],
COLON: ['', ['terminal', /:/], 'Spacing'],
COLON_NOT: ['', ['terminal', /:!/], 'Spacing'],
Spacing: ['*', ['/', 'Space', 'Comment']],
Space: ['terminal', /\s+/],
Comment: ['terminal', /\/\/.*?$/],
};
class PegParser {
constructor() {
const builder = new InitialPegBuilder_1.InitialPegBuilder();
const rules = builder.build(grammar);
this.pegInterpreter = new PackratParser_1.PackratParser(rules);
}
parse(s) {
return this.pegInterpreter.parse(s, 'grammar');
}
}
exports.PegParser = PegParser;
//# sourceMappingURL=PegParser.js.map