Skip to content

Commit

Permalink
Fix parser generation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mcchatman8009 committed Aug 23, 2018
1 parent 9c66418 commit 460eb44
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "antlr4-tool",
"version": "1.0.19",
"version": "1.0.20",
"description": "An Antlr4 tool that generates both Typescript and JavaScript Antlr Parsers",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
25 changes: 15 additions & 10 deletions src/antlr-core/parser-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from 'path';
import * as fs from 'fs';
import * as _ from 'lodash';
import * as util from './util';

const _eval = require('node-eval');

export function readLexer(grammar: string, lexerFile: string) {
Expand Down Expand Up @@ -68,18 +69,21 @@ export function parserMethods(parser: any) {
const symbols = symbolSet(parser);
const obj = {};

const methods = util.getNoArgMethods(parser);
const methods = util.getMethods(parser);

return _.map(methods, (method) => {
const methodObj = {} as any;
methodObj.name = method;
methodObj.name = method.name;

if (ruleToContextMap.has(method)) {
methodObj.type = ruleToContextMap.get(method);
} else if (symbols.has(method)) {
if (ruleToContextMap.has(method.name)) {
methodObj.type = ruleToContextMap.get(method.name);
methodObj.args = method.args;
} else if (symbols.has(method.name)) {
methodObj.type = 'TerminalNode';
methodObj.args = method.args;
} else {
methodObj.type = 'any';
methodObj.args = method.args;
}

return methodObj;
Expand Down Expand Up @@ -117,14 +121,15 @@ export function contextObjectAst(parser: any) {
const obj = {} as any;
obj.name = context.name;

const methods = _.filter(util.getNoArgMethods(context.prototype), (mth) => mth !== 'depth');
const methods = _.filter(util.getMethods(context.prototype), (mth) => mth !== 'depth');
obj.methods = _.map(methods, (method) => {
const methodObj = {} as any;
methodObj.name = method;
methodObj.name = method.name;
methodObj.args = method.args;

if (ruleToContextMap.has(method)) {
methodObj.type = ruleToContextMap.get(method);
} else if (symbols.has(method)) {
if (ruleToContextMap.has(method.name)) {
methodObj.type = ruleToContextMap.get(method.name);
} else if (symbols.has(method.name)) {
methodObj.type = 'TerminalNode';
} else {
methodObj.type = 'any';
Expand Down
2 changes: 1 addition & 1 deletion src/antlr-core/templates/parser.d.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export declare class <%= className %> extends Parser {

constructor(input: CommonTokenStream);
<% _.each(methods, (method) => { %>
<%= method.name %>(): <%= method.type%>;
<%= method.name %>(<%= method.args %>): <%= method.type%>;
<% }); %>
}
12 changes: 6 additions & 6 deletions src/antlr-core/util.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import * as path from 'path';
import * as _ from 'lodash';

export function getNoArgMethods(obj: any): any[] {
export function getMethods(obj: any): any[] {
const result: any[] = [];

_.each(obj, (val, id) => {
/* tslint:disable */
for (const id in obj) {
try {
if (typeof(obj[id]) === 'function' && obj[id].length === 0) {
result.push(id);
const mth = {name: id, args: ''};
result.push(mth);
}
} catch (err) {
}
});
}

return result;
}
Expand Down

0 comments on commit 460eb44

Please sign in to comment.