Skip to content

Commit

Permalink
Added more type information; Passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Worth committed Jan 23, 2018
1 parent 2047ef6 commit ac79b11
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 49 deletions.
4 changes: 2 additions & 2 deletions examples/arithmetics.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

Expression
= head:Term tail:(_ ("+" / "-") _ Term)* {
return tail.reduce(function(result, element) {
return tail.reduce(function(result: number, element: any[]) {
if (element[1] === "+") { return result + element[3]; }
if (element[1] === "-") { return result - element[3]; }
}, head);
}

Term
= head:Factor tail:(_ ("*" / "/") _ Factor)* {
return tail.reduce(function(result, element) {
return tail.reduce(function(result: number, element: any[]) {
if (element[1] === "*") { return result * element[3]; }
if (element[1] === "/") { return result / element[3]; }
}, head);
Expand Down
112 changes: 67 additions & 45 deletions src/passes/generate-ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function generateTS(ast, options) {
if (options.cache) {
parts.push([
"const key = peg$currPos * " + ast.rules.length + " + " + ruleIndexCode + ";",
"const cached = peg$resultsCache[key];",
"const cached: ICached = peg$resultsCache[key];",
"",
"if (cached) {",
" peg$currPos = cached.nextPos;",
Expand Down Expand Up @@ -920,61 +920,83 @@ function generateTS(ast, options) {

if (options.trace) {
parts.push([
"export function peg$DefaultTracer() {",
" this.indentLevel = 0;",
"interface ITraceEvent {",
" type: string;",
" rule: string;",
" result?: any;",
" location: IFileRange;",
"}",
"",
"peg$DefaultTracer.prototype.trace = function(event) {",
" const that = this;",
"export class DefaultTracer {",
" private indentLevel: number;",
"",
" function log(evt) {",
" function repeat(text, n) {",
" let result = \"\", i;",
" constructor() {",
" this.indentLevel = 0;",
" }",
"",
" for (i = 0; i < n; i++) {",
" result += text;",
" }",
" public trace(event: ITraceEvent) {",
" const that = this;",
"",
" return result;",
" }",
" function log(evt: ITraceEvent) {",
" function repeat(text: string, n: number) {",
" let result = \"\", i;",
"",
" function pad(text, length) {",
" return text + repeat(\" \", length - text.length);",
" }",
" for (i = 0; i < n; i++) {",
" result += text;",
" }",
"",
" return result;",
" }",
"",
" if (typeof console === \"object\") {", // IE 8-10
" console.log(",
" evt.location.start.line + \":\" + evt.location.start.column + \"-\"",
" + evt.location.end.line + \":\" + evt.location.end.column + \" \"",
" + pad(evt.type, 10) + \" \"",
" + repeat(\" \", that.indentLevel) + evt.rule",
" );",
" function pad(text: string, length: number) {",
" return text + repeat(\" \", length - text.length);",
" }",
"",
" if (typeof console === \"object\") {", // IE 8-10
" console.log(",
" evt.location.start.line + \":\" + evt.location.start.column + \"-\"",
" + evt.location.end.line + \":\" + evt.location.end.column + \" \"",
" + pad(evt.type, 10) + \" \"",
" + repeat(\" \", that.indentLevel) + evt.rule",
" );",
" }",
" }",
" }",
"",
" switch (event.type) {",
" case \"rule.enter\":",
" log(event);",
" this.indentLevel++;",
" break;",
" switch (event.type) {",
" case \"rule.enter\":",
" log(event);",
" this.indentLevel++;",
" break;",
"",
" case \"rule.match\":",
" this.indentLevel--;",
" log(event);",
" break;",
" case \"rule.match\":",
" this.indentLevel--;",
" log(event);",
" break;",
"",
" case \"rule.fail\":",
" this.indentLevel--;",
" log(event);",
" break;",
" case \"rule.fail\":",
" this.indentLevel--;",
" log(event);",
" break;",
"",
" default:",
" throw new Error(\"Invalid event type: \" + event.type + \".\");",
" default:",
" throw new Error(\"Invalid event type: \" + event.type + \".\");",
" }",
" }",
"};",
"}",
""
].join("\n"));
}

if (options.cache) {
parts.push([
"interface ICached {",
" nextPos: number;",
" result: any;",
"}",
"",
].join("\n"));
}

parts.push([
"function peg$parse(input: string, options?: IParseOptions) {",
" options = options !== undefined ? options : {};",
Expand Down Expand Up @@ -1026,7 +1048,7 @@ function generateTS(ast, options) {

if (options.cache) {
parts.push([
" const peg$resultsCache = {};",
" const peg$resultsCache: {[id: number]: ICached} = {};",
""
].join("\n"));
}
Expand All @@ -1046,7 +1068,7 @@ function generateTS(ast, options) {
}

parts.push([
" const peg$tracer = \"tracer\" in options ? options.tracer : new peg$DefaultTracer();",
" const peg$tracer = \"tracer\" in options ? options.tracer : new DefaultTracer();",
""
].join("\n"));
}
Expand Down Expand Up @@ -1325,14 +1347,14 @@ function generateTS(ast, options) {
return options.trace ?
[
"{",
" peg$SyntaxError as SyntaxError,",
" peg$DefaultTracer as DefaultTracer,",
" SyntaxError as SyntaxError,",
" DefaultTracer as DefaultTracer,",
" peg$parse as parse",
"}"
].join("\n") :
[
"{",
" peg$SyntaxError as SyntaxError,",
" SyntaxError as SyntaxError,",
" peg$parse as parse",
"}"
].join("\n");
Expand Down
6 changes: 4 additions & 2 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"tslint:recommended"
],
"jsRules": {},
"rules": {},
"rules": {
"max-classes-per-file": false
},
"rulesDirectory": []
}
}

0 comments on commit ac79b11

Please sign in to comment.