parsing

2019-08-14 admin

parsing是什么

什么是parsing,JSON grammar-based parser

parsing使用教程帮助文档

Parsing

Build Status

A JSON-esque grammar-based parser.

Getting started

$ npm install parsing
$ node
/**
 * Based on this EBNF grammar
 * - from http://stackoverflow.com/questions/6805172/how-do-you-abstract-some-expression-to-bnf#answer-6805185
 *
 * AEXP => AS+
 * AS   => id ':=' EX1 ';'
 * EX1  => EX2 (('+' | '-') EX2)*
 * EX2  => EX3 (('*' | '/') EX3)*
 * EX3  => EX4 ('^' EX3)*
 * EX4  => ('+'|'-')? EX5
 * EX5  => id | number | '(' EX1 ')'
 */
var grammarSpec = {
    ignore: 'whitespace',
    rules: {
        'assign': /:=/,
        'character': /[;*\/^+-]/,
        'id': /[\w$][\w\d$]*/,
        'number': /\d(?:\.\d+)?/,
        'whitespace': /\s+/,
        'AEXP': {
            components: {name: 'assignment', oneOrMoreOf: 'AS'}
        },
        'AS': {
            components: [{name: 'target', what: 'id'}, 'assign', {name: 'expression', what: 'EX1'}, {'character': ';'}]
        },
        'EX1': {
            captureAs: 'EX',
            components: [{name: 'left', what: 'EX2'}, {name: 'right', zeroOrMoreOf: [{name: 'operator', oneOf: [{'character': '+'}, {'character': '-'}]}, {name: 'operand', what: 'EX2'}]}],
            ifNoMatch: {component: 'right', capture: 'left'}
        },
        'EX2': {
            captureAs: 'EX',
            components: [{name: 'left', what: 'EX3'}, {name: 'right', zeroOrMoreOf: [{name: 'operator', oneOf: [{'character': '*'}, {'character': '/'}]}, {name: 'operand', what: 'EX3'}]}],
            ifNoMatch: {component: 'right', capture: 'left'}
        },
        'EX3': {
            captureAs: 'EX',
            components: [{name: 'left', what: 'EX4'}, {name: 'right', zeroOrMoreOf: [{name: 'operator', what: {'character': '^'}}, {name: 'operand', rule: 'EX3'}]}],
            ifNoMatch: {component: 'right', capture: 'left'}
        },
        'EX4': {
            captureAs: 'EX',
            components: [{name: 'operator', optionally: {oneOf: [{'character': '+'}, {'character': '-'}]}}, {name: 'operand', what: 'EX5'}],
            ifNoMatch: {component: 'operator', capture: 'operand'}
        },
        'EX5': {
            components: [{oneOf: ['id', 'number', [{'character': '('}, 'EX1', {'character': ')'}]]}]
        }
    },
    start: 'AEXP'
};

var parser = require('parsing').create(grammarSpec);

console.log(parser.parse('waldo:=1;'));
/**
 * Gives:
 * {
 *     name: 'AEXP',
 *     assignment: [{
 *         name: 'AS',
 *         target: 'waldo',
 *         expression: '1'
 *     }]
 * }
 */

Extending a grammar / Defining custom rules

Sometimes it can be handy to extend an existing grammar by defining custom rules and overriding some existing ones. For example, given this simple grammar we could add a new statement type to it:

var grammarSpec = {
    ignore: 'whitespace',
    rules: {
        'go_statement': {
            components: [{what: /go/, allowMerge: false}]
        },
        'end_statement': {
            components: [{what: /end/, allowMerge: false}]
        },
        'whitespace': /\s+/,
        'single_statement': {
            components: {oneOf: ['go_statement', 'end_statement']}
        },
        'statement': {
            components: ['single_statement', /;/]
        },
        'program': {
            components: {name: 'statements', zeroOrMoreOf: 'statement'}
        }
    },
    start: 'program'
};

// Note the third `options` argument here, which we use to pass the custom rule specs,
// adding support for the new `do_something_custom;` statement to the above grammar
var parser = require('parsing').create(grammarSpec, null, {
    rules: {
        'do_something_statement': {
            components: [{what: /do_something_custom/, allowMerge: false}]
        },
        'single_statement': {
            // Override `single_statement`, but refer back to the original
            components: {oneOf: ['do_something_statement', 'single_statement']}
        }
    }
});

console.log(parser.parse('go; do_something_custom; end;'));
/**
 * Gives:
 * {
 *     name: 'program',
 *     statements: [
 *         { name: 'go_statement' },
 *         { name: 'do_something_statement' },
 *         { name: 'end_statement' }
 *     ]
 * }
 */

Keeping up to date

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-72474.html

文章标题:parsing

相关文章
Parsing URL hash/fragment identifier with JavaScript
Yarin提出了一个问题:Parsing URL hash/fragment identifier with JavaScript,或许与您遇到的问题类似。 回答者YarinHovis给出了该问题的处理方式: Check out: jQue...
2018-04-10
Parsing JSON objects for HTML table
isherwooduser2478342提出了一个问题:Parsing JSON objects for HTML table,或许与您遇到的问题类似。 回答者pmandell给出了该问题的处理方式: Loop over each obje...
2018-04-10
回到顶部