本文整理匯總了TypeScript中nearley.Parser類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Parser類的具體用法?TypeScript Parser怎麽用?TypeScript Parser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Parser類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: catch
const processInput = _.debounce(() => {
if(!grammar) return;
try {
const parser = new nearley.Parser(grammar.ParserRules, grammar.ParserStart);
parser.feed(input.val());
output.text(JSON.stringify(parser.results[0]) || '');
console.log(parser.results);
} catch (e) {
output.text(`Error at character ${e.offset}`);
}
}, 1000);
示例2: catch
export const parse = (grammar: nearley.Grammar, input: string) : Result<any> => {
const parser = new Nearley.Parser(grammar.ParserRules, grammar.ParserStart);
try {
parser.feed(input);
} catch (e) {
return {error: e.stack};
}
return {result: parser.results.last()};
};
示例3: Parser
import { Parser, Grammar, Rule, Lexer } from 'nearley';
declare const parserRules: Rule[];
declare const parserStart: string;
declare const lexer: Lexer;
declare const grammar: Grammar;
let parser = new Parser(parserRules, parserStart, { lexer, keepHistory: false });
parser = new Parser(grammar);
try {
parser.feed("<123>");
if (parser.results) {
console.log(parser.results[0]);
}
} catch (error) {
console.log(error);
}
示例4: Grammar
rule.postprocess; // $ExpectType Postprocessor | undefined
rule.toString(); // $ExpectType string
rule.toString(1);
Grammar.fromCompiled(compiledRules); // $ExpectType Grammar
const grammar = new Grammar([rule]);
grammar.rules; // $ExpectType Rule[]
grammar.start; // $ExpectType string
grammar.byName; // $ExpectType { [ruleName: string]: Rule[]; }
grammar.lexer; // $ExpectType Lexer | undefined
Parser.fail; // $ExpectType {}
const parser = new Parser(grammar);
new Parser(grammar, {lexer});
new Parser(grammar, {keepHistory: false});
parser.grammar; // $ExpectType Grammar
parser.options; // $ExpectType ParserOptions
parser.lexer; // $ExpectType Lexer
parser.lexerState; // $ExpectType LexerState | undefined
parser.current; // $ExpectType number
parser.results; // $ExpectType any[]
parser.feed(''); // $ExpectType Parser
parser.finish(); // $ExpectType any[]
const state = parser.save();
state; // $ExpectType { [key: string]: any; lexerState: LexerState; }
parser.restore(state);
示例5:
export const parse = (text: string, grammar: CompiledRules): Parser => {
const noExtraSpaces = text.replace(/[\s]+/gi, ' ').trim();
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
parser.feed(noExtraSpaces);
return parser;
};