diff options
Diffstat (limited to 'javascripts/RegexParser.js')
-rw-r--r-- | javascripts/RegexParser.js | 66 |
1 files changed, 34 insertions, 32 deletions
diff --git a/javascripts/RegexParser.js b/javascripts/RegexParser.js index f2dd168..1f04c50 100644 --- a/javascripts/RegexParser.js +++ b/javascripts/RegexParser.js @@ -2,17 +2,15 @@ * RegexParser * */ -function RegexParser() { - this.alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWXYZ'; - this.str = this.regex = ''; - this.errorMessage = 'Ok' - this.errorPosition = -1; -} +function RegexParser() {} +RegexParser.prototype.getRegex = function() { return this.regex } +RegexParser.prototype.getErrorMessage = function() { return this.errorMessage } +RegexParser.prototype.getErrorPosition = function() { return this.errorPosition } RegexParser.prototype.lookahead = function() { if (this.str.length > 0) { - return this.str.substring(0,1); + return this.str.substring(0, 1); } return ''; } @@ -22,7 +20,7 @@ RegexParser.prototype.consume = function(symbol) { } RegexParser.prototype.trymatch = function(symbol) { - if (this.str.substring(0, 1) == symbol) { + if (this.str.substring(0, symbol.length) == symbol) { this.consume(symbol); return true; } @@ -31,12 +29,19 @@ RegexParser.prototype.trymatch = function(symbol) { RegexParser.prototype.match = function(symbol) { if (!this.trymatch(symbol)) { - throw('Expected symbol '+symbol); + throw("Expected symbol '"+symbol+"'."); } } +RegexParser.prototype.isIn = function(symbol, set) { + if (symbol.length > 0) { + return set.indexOf(symbol) >= 0; + } + return false; +} + RegexParser.prototype.isLetter = function(symbol) { - return this.alphabet.indexOf(symbol) >= 0; + return this.isIn(symbol, ALPHABET); } RegexParser.prototype.literal = function() { @@ -45,7 +50,7 @@ RegexParser.prototype.literal = function() { this.consume(symbol); return new Nfa(symbol); } - throw('Expected symbol or %.'); + throw("Expected a letter or '"+EMPTYSYMBOL+"'."); } RegexParser.prototype.atom = function() { @@ -58,12 +63,12 @@ RegexParser.prototype.atom = function() { } RegexParser.prototype.factor = function() { - return this.stars(this.atom()); + return this.star(this.atom()); } -RegexParser.prototype.stars = function(nfa) { +RegexParser.prototype.star = function(nfa) { if (this.trymatch('*')) { - return this.stars(nfa.star()); + return this.star(nfa.kleene()); } return nfa; } @@ -72,37 +77,34 @@ RegexParser.prototype.term = function() { var nfa = this.factor(); var symbol = this.lookahead(); if (this.isLetter(symbol) || (symbol == '(')) { - return nfa.concatenation(this.term()); + return nfa.concat(this.term()); } return nfa; } RegexParser.prototype.expr = function() { - var nfa = this.term(); + var nfa = this.term(); if (this.trymatch('|')) { return nfa.union(this.expr()); } return nfa; } -RegexParser.prototype.parse = function(str) { - var nfa; - this.str = this.regex = str; +RegexParser.prototype.parse = function(regex) { + this.str = regex; + this.errorMessage = 'Ok' + this.errorPosition = -1; + var nfa = null; - //try { + try { nfa = this.expr(); - if(this.str.length>0) { - throw('Supernumerous symbols'); + if(this.str.length > 0) { + throw('Supernumerous symbols.'); } - //} catch(e) { - // this.errorMessage = e; - // this.errorPosition = str.length - this.str.length; - // nfa = null; - //} + } catch(e) { + this.errorMessage = e; + this.errorPosition = regex.length - this.str.length; + nfa = null; + } return nfa; } - -RegexParser.prototype.getRegex = function() { return this.regex } -RegexParser.prototype.getErrorMessage = function() { return this.errorMessage } -RegexParser.prototype.getErrorPosition = function() { return this.errorPosition } - |