This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dfa.js
137 lines (100 loc) · 3.32 KB
/
dfa.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use strict";
function validate(definition) {
var error;
function forEachFinal(f) {
var i, result;
for (i = 0; i < definition.finals.length; i += 1) {
result = f(definition.finals[i]);
if (result) {
return result;
}
}
}
function forEachTransition(f) {
var state, character, destination, result;
for (state in definition.states) {
if (definition.states.hasOwnProperty(state)) {
for (character in definition.states[state]) {
if (definition.states[state].hasOwnProperty(character)) {
destination = definition.states[state][character];
result = f(state, character, destination);
if (result) {
return result;
}
}
}
}
}
}
function isStateUndefined(name) {
return definition.states.hasOwnProperty(name) === false;
}
// Definition is mandatory
if (!definition) {
return "Invalid definition";
}
// Start state is mandatory
if (!definition.start) {
return "Start state not specified";
}
// Final states are mandatory
if (!definition.finals) {
return "Final states not specified";
}
// At least one final state must be specified
if (definition.finals.length === 0) {
return "No final states listed";
}
// Definition of states must be specified
if (!definition.states) {
return "No states specified";
}
// Definition of start state must be specified
if (isStateUndefined(definition.start)) {
return "The initial state has no definition";
}
// Definition of every final state must be specified
error = forEachFinal(function (f) {
if (isStateUndefined(f)) {
return "Final state '" + f + "' has no definition";
}
});
if (error) {
return error;
}
// Each state must process one character at a time and the destination must have a
// definition
error = forEachTransition(function (state, character, destination) {
if (character.length !== 1) {
return "Invalid character '" + character + "' for state '" + state + "'";
}
if (isStateUndefined(destination)) {
return "State '" + state + "' has a non-existing destination '" + destination + "'";
}
});
if (error) {
return error;
}
}
exports.create = function (definition) {
var error;
error = validate(definition);
if (error) {
throw new Error(error);
}
return {
accept: function (s) {
var current, i;
if (typeof s !== "string") {
throw new Error("Input is not a string");
}
current = definition.start;
// During the process, current can be undefined because of unrecognized character
// for the current state. We can stop the loop as soon as current becomes undefined.
for (i = 0; i < s.length && current; i += 1) {
current = definition.states[current][s[i]];
}
return definition.finals.indexOf(current) !== -1;
}
};
};