mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-24 05:06:02 +08:00
Updated pegjs typings to also mirror parser api
This commit is contained in:
@@ -1,5 +1,49 @@
|
||||
/// <reference path="./pegjs.d.ts" />
|
||||
{
|
||||
let input: string;
|
||||
let result = PEG.parse(input);
|
||||
console.log(result);
|
||||
}
|
||||
|
||||
var input: string;
|
||||
var result = PEG.parse(input);
|
||||
console.log(result);
|
||||
import * as pegjs from 'pegjs';
|
||||
|
||||
{
|
||||
let pegparser: pegjs.Parser = pegjs.buildParser("start = ('a' / 'b')+");
|
||||
|
||||
try {
|
||||
let result: string = pegparser.parse("abba");
|
||||
} catch (error) {
|
||||
if (error instanceof pegparser.SyntaxError) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let parser = pegjs.buildParser("A = 'test'", {
|
||||
cache: true,
|
||||
allowedStartRules: ["A"],
|
||||
optimize: "speed",
|
||||
plugins: []
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
let parserOrSource: pegjs.Parser | string = pegjs.buildParser("A = 'test'", {output: "source"});
|
||||
} catch (error) {
|
||||
if (error instanceof pegjs.GrammarError) {
|
||||
let e: pegjs.GrammarError = error;
|
||||
} else if (error instanceof pegjs.parser.SyntaxError) {
|
||||
let e: pegjs.parser.SyntaxError = error;
|
||||
}
|
||||
|
||||
let e: pegjs.PegjsError = error;
|
||||
console.log(e.expected[0].description);
|
||||
console.log(e.expected[0].type);
|
||||
console.log(e.expected[0].value);
|
||||
console.log(e.location.end.column);
|
||||
console.log(e.location.end.offset);
|
||||
console.log(e.location.end.line);
|
||||
console.log(e.message);
|
||||
console.log(e.name);
|
||||
}
|
||||
56
pegjs/pegjs.d.ts
vendored
56
pegjs/pegjs.d.ts
vendored
@@ -1,6 +1,6 @@
|
||||
// Type definitions for PEG.js
|
||||
// Project: http://pegjs.majda.cz/
|
||||
// Definitions by: vvakame <https://github.com/vvakame>
|
||||
// Definitions by: vvakame <https://github.com/vvakame>, Tobias Kahlert <https://github.com/SrTobi>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
declare module PEG {
|
||||
@@ -28,3 +28,57 @@ declare module PEG {
|
||||
message:string;
|
||||
}
|
||||
}
|
||||
|
||||
declare module "pegjs" {
|
||||
|
||||
type Location = PEG.Location;
|
||||
type LocationRange = PEG.LocationRange;
|
||||
|
||||
interface ExpectedItem {
|
||||
type: string;
|
||||
value?: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
interface PegjsError extends Error {
|
||||
name: string;
|
||||
message: string;
|
||||
location: LocationRange;
|
||||
found?: any;
|
||||
expected?: ExpectedItem[];
|
||||
stack?: any;
|
||||
}
|
||||
|
||||
type GrammarError = PegjsError;
|
||||
var GrammarError: any;
|
||||
|
||||
interface ParserOptions {
|
||||
startRule: string;
|
||||
tracer: any;
|
||||
}
|
||||
|
||||
interface Parser {
|
||||
parse(input: string, options?:ParserOptions): any;
|
||||
|
||||
SyntaxError: any;
|
||||
}
|
||||
|
||||
interface BuildOptions {
|
||||
cache?: boolean;
|
||||
allowedStartRules?: string[];
|
||||
optimize?: string;
|
||||
plugins?: any[];
|
||||
}
|
||||
|
||||
interface OutputBuildOptions extends BuildOptions {
|
||||
output?: string;
|
||||
}
|
||||
|
||||
function buildParser(grammar: string, options?: BuildOptions): Parser;
|
||||
function buildParser(grammar: string, options?: OutputBuildOptions): Parser | string;
|
||||
|
||||
module parser {
|
||||
type SyntaxError = PegjsError;
|
||||
var SyntaxError: any;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user