Commit | Line | Data |
---|---|---|
f63072f5 DO |
1 | <?php |
2 | ||
3 | // Complain about martian char. | |
4 | function abortLex1 ($state, $text, $pos) | |
5 | { | |
6 | echo "Error! Could not parse the following text (current state is '${state}'): "; | |
7 | echo substr ($text, 0, $pos); | |
8 | echo '<font color = red>->' . $text{$pos} . '<-</font>'; | |
9 | echo substr ($text, $pos + 1); | |
10 | die; | |
11 | } | |
12 | ||
13 | // Complain about martian keyword. | |
14 | function abortLex2 ($state, $word) | |
15 | { | |
16 | echo "Error! Could not parse word (current state is '${state}'): '${word}'"; | |
17 | die; | |
18 | } | |
19 | ||
20 | // Produce a list of lexems from the given text. | |
21 | function getLexFromCodetext ($text) | |
22 | { | |
23 | $ret = array(); | |
24 | $textlen = strlen ($text); | |
25 | $state = "ESOTSM"; | |
26 | for ($i = 0; $i < $textlen; $i++) : | |
27 | $char = $text{$i}; | |
28 | $newstate = $state; | |
29 | switch ($state) : | |
30 | case 'ESOTSM': | |
31 | switch (TRUE) | |
32 | { | |
33 | case ($char == ';'): | |
34 | $ret[] = array ('lexem' => 'SEMICOLON'); | |
35 | break; | |
36 | case ($char == '('): | |
37 | $ret[] = array ('lexem' => 'LBRACE'); | |
38 | break; | |
39 | case ($char == ')'): | |
40 | $ret[] = array ('lexem' => 'RBRACE'); | |
41 | break; | |
42 | case ($char == '#'): | |
43 | $newstate = 'skipping comment'; | |
44 | break; | |
45 | case (preg_match ('/^[a-zA-Z]$/', $char)): | |
46 | $newstate = 'reading word'; | |
47 | $buffer = $char; | |
48 | break; | |
49 | case (preg_match ('/^[ \t\n]$/', $char)): | |
50 | // nom-nom... | |
51 | break; | |
52 | case ($char == '{'): | |
53 | $newstate = 'reading tag 1'; | |
54 | break; | |
55 | case ($char == '['): | |
56 | $newstate = 'reading predicate 1'; | |
57 | break; | |
58 | default: | |
59 | abortLex1 ($state, $text, $i); | |
60 | } | |
61 | break; | |
62 | case 'reading word': | |
63 | switch (TRUE) | |
64 | { | |
65 | case (preg_match ('/^[a-zA-Z]$/', $char)): | |
66 | $buffer .= $char; | |
67 | break; | |
68 | case (preg_match ('/^[ \t\n]$/', $char)): | |
69 | // got a word, sort it out | |
70 | switch ($buffer) | |
71 | { | |
72 | case 'allow': | |
73 | $ret[] = array ('lexem' => 'ALLOW'); | |
74 | break; | |
75 | case 'deny': | |
76 | $ret[] = array ('lexem' => 'DENY'); | |
77 | break; | |
78 | case 'define': | |
79 | $ret[] = array ('lexem' => 'DEFINE'); | |
80 | break; | |
81 | case 'and': | |
82 | $ret[] = array ('lexem' => 'AND'); | |
83 | break; | |
84 | case 'or': | |
85 | $ret[] = array ('lexem' => 'OR'); | |
86 | break; | |
87 | default: | |
88 | abortLex2 ($state, $buffer); | |
89 | } | |
90 | $newstate = 'ESOTSM'; | |
91 | break; | |
92 | default: | |
93 | abortLex1 ($state, $text, $i); | |
94 | } | |
95 | break; | |
96 | case 'reading tag 1': | |
97 | switch (TRUE) | |
98 | { | |
99 | case (preg_match ('/^[ \t\n]$/', $char)): | |
100 | // nom-nom... | |
101 | break; | |
102 | case (preg_match ('/^[a-zA-Z\$]$/', $char)): | |
103 | $buffer = $char; | |
104 | $newstate = 'reading tag 2'; | |
105 | break; | |
106 | default: | |
107 | abortLex1 ($state, $text, $i); | |
108 | } | |
109 | break; | |
110 | case 'reading tag 2': | |
111 | switch (TRUE) | |
112 | { | |
113 | case ($char == '}'): | |
114 | $ret[] = array ('lexem' => 'TAG', 'load' => rtrim ($buffer)); | |
115 | $newstate = 'ESOTSM'; | |
116 | break; | |
117 | case (preg_match ('/^[a-zA-Z0-9 _-]$/', $char)): | |
118 | $buffer .= $char; | |
119 | break; | |
120 | default: | |
121 | abortLex1 ($state, $text, $i); | |
122 | } | |
123 | break; | |
124 | case 'reading predicate 1': | |
125 | switch (TRUE) | |
126 | { | |
127 | case (preg_match ('/^[ \t\n]$/', $char)): | |
128 | // nom-nom... | |
129 | break; | |
130 | case (preg_match ('/^[a-zA-Z]$/', $char)): | |
131 | $buffer = $char; | |
132 | $newstate = 'reading predicate 2'; | |
133 | break; | |
134 | default: | |
135 | abortLex1 ($state, $text, $i); | |
136 | } | |
137 | break; | |
138 | case 'reading predicate 2': | |
139 | switch (TRUE) | |
140 | { | |
141 | case ($char == ']'): | |
142 | $ret[] = array ('lexem' => 'PREDICATE', 'load' => rtrim ($buffer)); | |
143 | $newstate = 'ESOTSM'; | |
144 | break; | |
145 | case (preg_match ('/^[a-zA-Z0-9 _-]$/', $char)): | |
146 | $buffer .= $char; | |
147 | break; | |
148 | default: | |
149 | abortLex1 ($state, $text, $i); | |
150 | } | |
151 | break; | |
152 | case 'skipping comment': | |
153 | switch ($text{$i}) | |
154 | { | |
155 | case "\n": | |
156 | $newstate = 'ESOTSM'; | |
157 | default: // eat char, nom-nom... | |
158 | break; | |
159 | } | |
160 | break; | |
161 | default: | |
162 | die (__FUNCTION__ . "(): internal error, state == ${state}"); | |
163 | endswitch; | |
164 | $state = $newstate; | |
165 | endfor; | |
166 | return $ret; | |
167 | } | |
168 | ||
169 | ?> |