Skip to content

Commit 88db719

Browse files
committed
Updated pegjs typings to also mirror parser api
1 parent 4ba57e6 commit 88db719

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

pegjs/pegjs-tests.ts

+47-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
11
/// <reference path="./pegjs.d.ts" />
2+
{
3+
let input: string;
4+
let result = PEG.parse(input);
5+
console.log(result);
6+
}
27

3-
var input: string;
4-
var result = PEG.parse(input);
5-
console.log(result);
8+
import * as pegjs from 'pegjs';
9+
10+
{
11+
let pegparser: pegjs.Parser = pegjs.buildParser("start = ('a' / 'b')+");
12+
13+
try {
14+
let result: string = pegparser.parse("abba");
15+
} catch (error) {
16+
if (error instanceof pegparser.SyntaxError) {
17+
18+
}
19+
}
20+
}
21+
22+
{
23+
let parser = pegjs.buildParser("A = 'test'", {
24+
cache: true,
25+
allowedStartRules: ["A"],
26+
optimize: "speed",
27+
plugins: []
28+
})
29+
}
30+
31+
try {
32+
let parserOrSource: pegjs.Parser | string = pegjs.buildParser("A = 'test'", {output: "source"});
33+
} catch (error) {
34+
if (error instanceof pegjs.GrammarError) {
35+
let e: pegjs.GrammarError = error;
36+
} else if (error instanceof pegjs.parser.SyntaxError) {
37+
let e: pegjs.parser.SyntaxError = error;
38+
}
39+
40+
let e: pegjs.PegjsError = error;
41+
console.log(e.expected[0].description);
42+
console.log(e.expected[0].type);
43+
console.log(e.expected[0].value);
44+
console.log(e.location.end.column);
45+
console.log(e.location.end.offset);
46+
console.log(e.location.end.line);
47+
console.log(e.message);
48+
console.log(e.name);
49+
}

pegjs/pegjs.d.ts

+55-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Type definitions for PEG.js
22
// Project: http://pegjs.majda.cz/
3-
// Definitions by: vvakame <https://github.com/vvakame>
3+
// Definitions by: vvakame <https://github.com/vvakame>, Tobias Kahlert <https://github.com/SrTobi>
44
// Definitions: https://github.com/borisyankov/DefinitelyTyped
55

66
declare module PEG {
@@ -28,3 +28,57 @@ declare module PEG {
2828
message:string;
2929
}
3030
}
31+
32+
declare module "pegjs" {
33+
34+
type Location = PEG.Location;
35+
type LocationRange = PEG.LocationRange;
36+
37+
interface ExpectedItem {
38+
type: string;
39+
value?: string;
40+
description: string;
41+
}
42+
43+
interface PegjsError extends Error {
44+
name: string;
45+
message: string;
46+
location: LocationRange;
47+
found?: any;
48+
expected?: ExpectedItem[];
49+
stack?: any;
50+
}
51+
52+
type GrammarError = PegjsError;
53+
var GrammarError: any;
54+
55+
interface ParserOptions {
56+
startRule: string;
57+
tracer: any;
58+
}
59+
60+
interface Parser {
61+
parse(input: string, options?:ParserOptions): any;
62+
63+
SyntaxError: any;
64+
}
65+
66+
interface BuildOptions {
67+
cache?: boolean;
68+
allowedStartRules?: string[];
69+
optimize?: string;
70+
plugins?: any[];
71+
}
72+
73+
interface OutputBuildOptions extends BuildOptions {
74+
output?: string;
75+
}
76+
77+
function buildParser(grammar: string, options?: BuildOptions): Parser;
78+
function buildParser(grammar: string, options?: OutputBuildOptions): Parser | string;
79+
80+
module parser {
81+
type SyntaxError = PegjsError;
82+
var SyntaxError: any;
83+
}
84+
}

0 commit comments

Comments
 (0)