From 88db719064cc9f7d146794a23355e7f4a890beb4 Mon Sep 17 00:00:00 2001 From: SrTobi Date: Wed, 2 Mar 2016 22:01:29 +0100 Subject: [PATCH] Updated pegjs typings to also mirror parser api --- pegjs/pegjs-tests.ts | 50 ++++++++++++++++++++++++++++++++++++--- pegjs/pegjs.d.ts | 56 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 4 deletions(-) diff --git a/pegjs/pegjs-tests.ts b/pegjs/pegjs-tests.ts index 90ebe2da1..082836e74 100644 --- a/pegjs/pegjs-tests.ts +++ b/pegjs/pegjs-tests.ts @@ -1,5 +1,49 @@ /// +{ + 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); +} \ No newline at end of file diff --git a/pegjs/pegjs.d.ts b/pegjs/pegjs.d.ts index 1e8bfc6ad..00f4c7316 100644 --- a/pegjs/pegjs.d.ts +++ b/pegjs/pegjs.d.ts @@ -1,6 +1,6 @@ // Type definitions for PEG.js // Project: http://pegjs.majda.cz/ -// Definitions by: vvakame +// Definitions by: vvakame , Tobias Kahlert // 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; + } +} \ No newline at end of file