當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript nearley.Parser類代碼示例

本文整理匯總了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);
開發者ID:shnud94,項目名稱:livelang,代碼行數:11,代碼來源:test.ts

示例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()};
};
開發者ID:shnud94,項目名稱:livelang,代碼行數:11,代碼來源:nearley.ts

示例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);
}
開發者ID:Crevil,項目名稱:DefinitelyTyped,代碼行數:18,代碼來源:nearley-tests.ts

示例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);
開發者ID:AbraaoAlves,項目名稱:DefinitelyTyped,代碼行數:31,代碼來源:nearley-tests.ts

示例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;
};
開發者ID:theseyi,項目名稱:WhereHows,代碼行數:6,代碼來源:parser.ts


注:本文中的nearley.Parser類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。