Beginning to add some functionality to the parser

This commit is contained in:
Jeff Avallone 2014-11-26 18:24:40 -05:00
parent c6f2271867
commit facb99e8bc
6 changed files with 70 additions and 7 deletions

View File

@ -1,7 +1,28 @@
import parser from 'src/js/parser/javascript.peg'; import parser from 'src/js/parser/javascript.js';
describe('parser/javascript.peg', function() { describe('parser/javascript.peg', function() {
pending(); describe('regular expression literals', function() {
describe('flags support', function() {
it('handles the ignore case flag', function() {
expect(parser.parse('/test/i').flags().ignore_case).toEqual(true);
expect(parser.parse('/test/').flags().ignore_case).toEqual(false);
});
it('handles the global flag', function() {
expect(parser.parse('/test/g').flags().global).toEqual(true);
expect(parser.parse('/test/').flags().global).toEqual(false);
});
it('handles the multiline flag', function() {
expect(parser.parse('/test/m').flags().multiline).toEqual(true);
expect(parser.parse('/test/').flags().multiline).toEqual(false);
});
});
});
}); });

View File

@ -1,3 +1,3 @@
import parser from './parser/javascript.peg'; import parser from './parser/javascript.js';
window.parser = parser; window.parser = parser;

View File

@ -0,0 +1,9 @@
import parser from './javascript/grammar.peg';
import Root from './javascript/root.js';
import Regexp from './javascript/regexp.js';
parser.Parser.Root = Root;
parser.Parser.Regexp = Regexp;
export default parser;

View File

@ -1,7 +1,6 @@
grammar JavascriptRegexp grammar JavascriptRegexp
root <- regexp_literal / regexp root <- ( ( "/" regexp "/" fl:[igm]* ) / regexp ) <Root>
regexp_literal <- "/" regexp "/" [igm]* regexp <- match ( "|" regexp )? <Regexp>
regexp <- match ( "|" regexp )?
match <- anchor_start? ( ( subexp / charset / terminal ) repeat? )* anchor_end? match <- anchor_start? ( ( subexp / charset / terminal ) repeat? )* anchor_end?
anchor_start <- "^" anchor_start <- "^"
anchor_end <- "$" anchor_end <- "$"

View File

@ -0,0 +1,9 @@
export default {
matches() {
if (this.elements[1].regexp) {
return [this.match].concat(this.elements[1].regexp.matches());
} else {
return [this.match];
}
}
};

View File

@ -0,0 +1,25 @@
export default {
flags() {
var flags;
if (this.fl) {
flags = this.fl.textValue;
} else {
flags = '';
}
return {
global: /g/.test(flags),
ignore_case: /i/.test(flags),
multiline: /m/.test(flags)
};
},
expression() {
if (this.regexp) {
return this.regexp;
} else {
return this;
}
}
};