本文整理汇总了TypeScript中pegjs.buildParser函数的典型用法代码示例。如果您正苦于以下问题:TypeScript buildParser函数的具体用法?TypeScript buildParser怎么用?TypeScript buildParser使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了buildParser函数的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: catch
documents.onDidChangeContent((change) => {
let diagnostics: Diagnostic[] = [];
try {
let result = pegjs.buildParser(change.document.getText());
} catch(error)
{
let err : pegjs.PegjsError = error;
diagnostics.push({
severity: DiagnosticSeverity.Error,
range: pegjsLoc_to_vscodeRange(err.location),
message: error.name + ": " + error.message
});
}
// Send the computed diagnostics to VS Code.
connection.sendDiagnostics({ uri: change.document.uri, diagnostics });
});
示例2: catch
import * as pegjs from 'pegjs';
{
let input: string;
let result = pegjs.PEG.parse(input);
console.log(result);
}
{
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) {
示例3: pJoin
start
= Expression
Expression
= Join
/ Terminal
Join
= left:Terminal right:Expression { return pJoin(left, right) }
Terminal
= Param
/ Num
Param "param"
= char:[a-zA-Z] { return pParam(char) }
Num "num"
= digits:\[0-9]*\.\[0-9]+ { return pNum(digits) }
/ digits:\[0-9]+ { return pNum(digits) }
`
export const parse = buildParser(grammar).parse
export const parse2 = buildParser(grammar2).parse
console.log('parse2', parse2('abc'))
示例4: require
ďťżimport path = require('path');
import fs = require('fs');
var peg = require('pegjs');
// Create a global variable that the code inside the PEG grammar can access.
global.pegopts = 'options';
// Create a parser for typescript model definitions (using pegjs).
var grammar = fs.readFileSync(path.join(__dirname, './ts.peg'), 'utf8');
var parser = peg.buildParser(grammar);
/** Generate a schema corresponding to the declarations in the given typescript file. */
export function parse(source: string) {
var ast = parser.parse(source);
return ast;
}