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() {
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;

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,8 +1,7 @@
grammar JavascriptRegexp
root <- regexp_literal / regexp
regexp_literal <- "/" regexp "/" [igm]*
regexp <- match ( "|" regexp )?
match <- anchor_start? ( ( subexp / charset / terminal ) repeat? )* anchor_end?
root <- ( ( "/" regexp "/" fl:[igm]* ) / regexp ) <Root>
regexp <- match ( "|" regexp )? <Regexp>
match <- anchor_start? ( ( subexp / charset / terminal ) repeat? )* anchor_end?
anchor_start <- "^"
anchor_end <- "$"
repeat <- ( repeat_any / repeat_required / repeat_optional / repeat_spec ) repeat_greedy?

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;
}
}
};