summaryrefslogtreecommitdiff
path: root/javascripts/RegexParser.js
blob: 1f04c5032fb34fe96cc7bafd2cdf166bb3ac68cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * RegexParser
 *
 */
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 '';
}

RegexParser.prototype.consume = function(symbol) {
    this.str = this.str.substring(1);
}

RegexParser.prototype.trymatch = function(symbol) {
    if (this.str.substring(0, symbol.length) == symbol) {
        this.consume(symbol);
        return true;
    }
    return false;
}

RegexParser.prototype.match = function(symbol) {
    if (!this.trymatch(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.isIn(symbol, ALPHABET);
}

RegexParser.prototype.literal = function() {
    var symbol = this.lookahead();
    if(this.isLetter(symbol)) {
        this.consume(symbol);
        return new Nfa(symbol);
    }
    throw("Expected a letter or '"+EMPTYSYMBOL+"'."); 
}

RegexParser.prototype.atom = function() {
    if(this.trymatch('(')) {
        var nfa = this.expr();
        this.match(')');
        return nfa;
    }
    return this.literal();
}

RegexParser.prototype.factor = function() {
    return this.star(this.atom());
}

RegexParser.prototype.star = function(nfa) {
    if (this.trymatch('*')) {
        return this.star(nfa.kleene());
    }
    return nfa;
}

RegexParser.prototype.term = function() {
    var nfa = this.factor();
    var symbol = this.lookahead();
    if (this.isLetter(symbol) || (symbol == '(')) {
        return nfa.concat(this.term());
    }
    return nfa;
}

RegexParser.prototype.expr = function() {
	var nfa = this.term();
    if (this.trymatch('|')) {
        return nfa.union(this.expr());
    }
    return nfa;
}

RegexParser.prototype.parse = function(regex) {
    this.str = regex;
    this.errorMessage  = 'Ok'
    this.errorPosition = -1;
    var nfa = null;

    try {
        nfa = this.expr();
        if(this.str.length > 0) {
            throw('Supernumerous symbols.');
        }
    } catch(e) {
        this.errorMessage = e;
		this.errorPosition = regex.length - this.str.length;        
        nfa = null;
    }
    return nfa;
}