Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ You can get an introductory overview of the tool in [this article](https://mediu
- [Rust plugin](#rust-plugin)
- [Java plugin](#java-plugin)
- [Julia plugin](#julia-plugin)
- [Lua plugin](#lua-plugin)
- [Grammar format](#grammar-format)
- [JSON-like notation](#json-like-notation)
- [Yacc/Bison notation](#yaccbison-notation)
Expand Down Expand Up @@ -321,6 +322,21 @@ For complex Julia parser implementations it is recommended to leverage the JSON-
}
```

#### Lua Plugin
Syntax supports Lua as a target language. See its [calculator example](https://github.com/DmitrySoshnikov/syntax/blob/master/examples/calc.lua.g):

```
./bin/syntax -g examples/calc.lua.g -m lalr1 -o calcparser.lua
```

Then callers can use the module as:

```lua
Parser = require("calcparser")
parser = Parser.new()
print(parser:parse("2^2^2^2")) -- 65536
```

### Grammar format

_Syntax_ support two main notations to define grammars: _JSON-like_ notation, and _Yacc/Bison-style_ notation.
Expand Down
40 changes: 40 additions & 0 deletions examples/calc.lua.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Generated parser in Lua.
*
* ./bin/syntax -g examples/calc.lua.g -m lalr1 -o calcparser.lua
*
* > Parser = require("calcparser")
* > parser = Parser.new()
* > print(parser:parse("2^2^2^2"))
* 65536
*/

{
"lex": {
"rules": [
["%s+", "-- skip whitespace"],
["%d+", "return 'NUMBER'"],
["%*", "return '*'"],
["%+", "return '+'"],
["%(", "return '('"],
["%)", "return ')'"],
["%^", "return '^'"],
]
},

"operators": [
["left", "+"],
["left", "*"],
["right", "^"],
],

"bnf": {
"E": [
["E + E", "$$ = $1 + $3"],
["E * E", "$$ = $1 * $3"],
["E ^ E", "$$ = $1 ^ $3"],
["NUMBER", "$$ = tonumber($1)"],
["( E )", "$$ = $2"],
],
},
}
4 changes: 3 additions & 1 deletion src/bin/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ const parsers = {
.default,
jl: require(ROOT + 'plugins/julia/lr/lr-parser-generator-julia.js')
.default,
lua: require(ROOT + 'plugins/lua/lr/lr-parser-generator-lua.js')
.default,
};

const LRParserGenerator = GENERATORS[language] || GENERATORS.js;
Expand Down Expand Up @@ -587,7 +589,7 @@ function getLexGrammarData(options) {

// If explicit lexical grammar file was passed, use it.
if (options.lex) {
data = Grammar.dataFromGrammarFile(options.lex, { grammarType: 'lex' });
data = Grammar.dataFromGrammarFile(options.lex, {grammarType: 'lex'});
}

if (options['ignore-whitespaces'] && !data) {
Expand Down
60 changes: 60 additions & 0 deletions src/plugins/lua/lr/lr-parser-generator-lua.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* The MIT License (MIT)
* Copyright (c) 2015-present Dmitry Soshnikov <[email protected]>
*/

const LRParserGeneratorDefault = require(ROOT + 'lr/lr-parser-generator-default').default;
const LuaParserGeneratorTrait = require('../lua-parser-generator-trait');

import fs from 'fs';
import path from 'path';

const LUA_LR_PARSER_TEMPLATE = fs.readFileSync(
`${__dirname}/../templates/lr.template.lua`,
'utf-8',
);

/**
* LR parser generator for Lua.
*/
export default class LRParserGeneratorLua extends LRParserGeneratorDefault {

/**
* Instance constructor.
*/
constructor({
grammar,
outputFile,
options = {},
}) {
super({grammar, outputFile, options})
.setTemplate(LUA_LR_PARSER_TEMPLATE);

/**
* Contains the lexical rule handlers: _lexRule1, _lexRule2, etc.
* It's populated by the trait file.
*/
this._lexHandlers = [];
this._productionHandlers = [];

/**
* Actual class name of your parser. Here we infer from the output filename.
*/
this._parserClassName = path.basename(
outputFile,
path.extname(outputFile),
);

Object.assign(this, LuaParserGeneratorTrait);
}

/**
* Generates parser code.
*/
generateParserData() {
super.generateParserData();
this.generateLexHandlers();
this.generateProductionHandlers();
this.generateParserClassName(this._parserClassName);
}
};
202 changes: 202 additions & 0 deletions src/plugins/lua/lua-parser-generator-trait.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/**
* The MIT License (MIT)
* Copyright (c) 2015-present Dmitry Soshnikov <[email protected]>
*/

import fs from 'fs';

const LUA_TOKENIZER_TEMPLATE = fs.readFileSync(
`${__dirname}/templates/tokenizer.template.lua`,
'utf-8'
);

const LuaParserGeneratorTrait = {

/**
* Generates parser class name.
*/
generateParserClassName(className) {
this.writeData('PARSER_CLASS_NAME', className);
},

generateParseTable() {
this.writeData(
'TABLE',
this._toLuaMap(this.generateParseTableData()),
);
},

/**
* Generates tokens table in Lua Map format.
*/
generateTokensTable() {
this.writeData(
'TOKENS',
this._toLuaMap(this._tokens),
);
},

buildSemanticAction(production) {
let action = this.getSemanticActionCode(production);

if (!action) {
return null;
}

action += ';';

const args = this
.getSemanticActionParams(production)
.join(',');

this._productionHandlers.push({args, action});
return `_handler${this._productionHandlers.length}`;
},

generateProductionsData() {
return this.generateRawProductionsData()
.map(data => {
return `{ ${data.map((item, index) => {
// quote
if (index >= 2) {
return `"${item}"`;
}
return item;
}).join(',')} }`;
});
},

generateBuiltInTokenizer() {
this.writeData('TOKENIZER', LUA_TOKENIZER_TEMPLATE);
},

generateLexRules() {
let lexRules = this._grammar.getLexGrammar().getRules().map(lexRule => {

const action = lexRule.getRawHandler() + ';';

this._lexHandlers.push({args: '', action});

const flags = [];

if (lexRule.isCaseInsensitive()) {
flags.push('i');
}

// Example: ["^\s+", "_lexRule1"],
return `{[[${lexRule.getRawMatcher()}${flags.join('')}]], ` +
`"_lexRule${this._lexHandlers.length}"}`;
});

this.writeData('LEX_RULES', `{ ${lexRules.join(',\n')} }`);
},

generateLexRulesByStartConditions() {
const lexGrammar = this._grammar.getLexGrammar();
const lexRulesByConditions = lexGrammar.getRulesByStartConditions();
const result = {};

for (const condition in lexRulesByConditions) {
result[condition] = lexRulesByConditions[condition].map(lexRule =>
lexGrammar.getRuleIndex(lexRule)
);
}

this.writeData(
'LEX_RULES_BY_START_CONDITIONS',
`${this._toLuaMap(result)}`,
);
},

/**
* Converts JS object to Lua's table representation.
* E.g. converts {foo: 10, bar: 20} into {foo = 10, bar = 20}
*/
_toLuaMap(value) {
function _toLuaMapInner(value) {
if (value === null) return "nil";
if (typeof value === "number" || typeof value === "boolean") return value.toString();
if (typeof value === "string") return `"${value.replace(/"/g, '\\"')}"`;

if (Array.isArray(value)) {
const items = value.map(_toLuaMapInner).join(", ");
return `{${items}}`;
}

if (typeof value === "object") {
const entries = Object.entries(value).map(([k, v]) => {
const key = /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(k) ? k : `["${k}"]`;
return `${key} = ${_toLuaMapInner(v)}`;
}).join(", ");
return `{${entries}}`;
}

return "nil"; // fallback
}

return _toLuaMapInner(value);
},

/**
* Lua lex rules handler declarations.
*/
generateLexHandlers() {
const handlers = this._generateHandlers(
this._lexHandlers,
'Tokenizer:',
'_lexRule',
'' /* return type, you can use e.g. 'string' */
);
this.writeData('LEX_RULE_HANDLERS', handlers.join('\n\n'));
},

/**
* Lua parser handler declarations.
*/
generateProductionHandlers() {
const handlers = this._generateHandlers(
this._productionHandlers,
'parser:',
'_handler',
'', /* return type */
);
this.writeData('PRODUCTION_HANDLERS', handlers.join('\n'));
},

/**
* Productions array in the Lua format.
*
* An array of arrays, see `generateProductionsData` for details.
*/
generateProductions() {
this.writeData(
'PRODUCTIONS',
`{ ${this.generateProductionsData().join(',\n')} }`
);
},

/**
* Injects the code passed in the module include directive.
*/
generateModuleInclude() {
let moduleInclude = this._grammar.getModuleInclude();

if (!moduleInclude) {
// Example: add some default module include if needed.
moduleInclude = `
let foo = 'Example module include';
`;
}

this.writeData('MODULE_INCLUDE', moduleInclude);
},

_generateHandlers(handlers, class_prefix, name, returnType = '') {
return handlers.map(({args, action}, index) => {
return `\nfunction ${class_prefix}${name}${index + 1}` +
`(${args})\n\t\t${action}\nend`
});
},
};

module.exports = LuaParserGeneratorTrait;
Loading