diff options
Diffstat (limited to 'javascripts/RegexParser.js')
-rw-r--r-- | javascripts/RegexParser.js | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/javascripts/RegexParser.js b/javascripts/RegexParser.js index 3abe426..42251af 100644 --- a/javascripts/RegexParser.js +++ b/javascripts/RegexParser.js @@ -1,14 +1,16 @@ /* * RegexParser - * + * Parsing a regular expression using recursive descent method. + * parse() returns a NFA (constructed following Thompson's algrotihm). */ function RegexParser() {}; +// Accessor functions RegexParser.prototype.getRegex = function() { return this.regex; }; RegexParser.prototype.getErrorMessage = function() { return this.errorMessage; }; RegexParser.prototype.getErrorPosition = function() { return this.errorPosition; }; -// +// Next symbol in regex. RegexParser.prototype.lookahead = function() { if (this.str.length > 0) { return this.str.substring(0, 1); @@ -16,12 +18,12 @@ RegexParser.prototype.lookahead = function() { return ''; }; -// +// Removing the firstmost symbol of regex. RegexParser.prototype.consume = function(symbol) { this.str = this.str.substring(1); }; -// +// Check if symbol matches expected symbol. RegexParser.prototype.trymatch = function(symbol) { if (this.str.substring(0, symbol.length) == symbol) { this.consume(symbol); @@ -30,14 +32,14 @@ RegexParser.prototype.trymatch = function(symbol) { return false; }; -// +// See above. RegexParser.prototype.match = function(symbol) { if (!this.trymatch(symbol)) { throw("RegexParser.match(): Expected symbol '"+symbol+"'."); }; }; -// +// Checks if a symbol is in set of symbols. RegexParser.prototype.isIn = function(symbol, set) { if (symbol.length > 0) { return set.indexOf(symbol) >= 0; @@ -45,12 +47,12 @@ RegexParser.prototype.isIn = function(symbol, set) { return false; }; -// +// See above. Set = alphabet. RegexParser.prototype.isLetter = function(symbol) { return this.isIn(symbol, ALPHABET); }; -// +// Is symbol a literal? RegexParser.prototype.literal = function() { var symbol = this.lookahead(); if(this.isLetter(symbol)) { @@ -60,7 +62,7 @@ RegexParser.prototype.literal = function() { throw("RegexParser.literal(): Expected a letter or '"+EMPTYSYMBOL+"'."); }; -// +// Atomar expression. RegexParser.prototype.atom = function() { if(this.trymatch('(')) { var nfa = this.expr(); @@ -70,12 +72,12 @@ RegexParser.prototype.atom = function() { return this.literal(); }; -// +// Factor. RegexParser.prototype.factor = function() { return this.star(this.atom()); }; -// +// Kleene Star. RegexParser.prototype.star = function(nfa) { if (this.trymatch('*')) { return this.star(nfa.kleene()); @@ -83,7 +85,7 @@ RegexParser.prototype.star = function(nfa) { return nfa; }; -// +// Term. RegexParser.prototype.term = function() { var nfa = this.factor(); var symbol = this.lookahead(); @@ -93,7 +95,7 @@ RegexParser.prototype.term = function() { return nfa; }; -// +// Expression. RegexParser.prototype.expr = function() { var nfa = this.term(); if (this.trymatch('|')) { @@ -102,7 +104,7 @@ RegexParser.prototype.expr = function() { return nfa; }; -// +// Parse function. var nfa; RegexParser.prototype.parse = function(regex) { this.str = regex; |