UNPKG

parsing

Version:
60 lines (55 loc) 1.91 kB
/* * Parsing - JSON grammar-based parser * Copyright (c) Dan Phillimore (asmblah) * http://asmblah.github.com/parsing/ * * Released under the MIT license * https://github.com/asmblah/parsing/raw/master/MIT-LICENSE.txt */ 'use strict'; var expect = require('chai').expect, Parser = require('../../../../src/Parser'); // Note that these are not the same as Processors, which operate on a Rule's root Component describe('Parser grammar component match modifier basic', function () { it('should support "what" component modifiers overriding the capture', function () { var grammarSpec = { ignore: 'whitespace', rules: { 'my_rule': { components: [{ name: 'my_capture', what: /my\s+\w+/, modifier: function (capture) { // Override the captured string return '[my prefix]' + capture + '[my suffix]'; } }] }, 'whitespace': /\s+/, }, start: 'my_rule', bounds: 'my_bounds' }, options = { captureAllBounds: true }, parser = new Parser(grammarSpec, null, options), code = ' my\n text '; expect(parser.parse(code)).to.deep.equal({ name: 'my_rule', my_capture: '[my prefix]my\n text[my suffix]', my_bounds: { start: { offset: 2, line: 1, column: 3 }, end: { offset: 10, line: 2, column: 6 } } }); }); });