tokn 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.txt +3 -0
- metadata +1 -2
- data/README2.txt +0 -219
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 804ed12fc717a528758a7f1bc2ec03e92e829310
|
4
|
+
data.tar.gz: 2d3df30d3525d0c0ef3d5b7aa1f16839db6fd786
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80fb1504a1f42d95ebe2ac6b97d16f31d9b14e2f1b3d2e08ab45780584f8b0763a2f1a4a2da09614be68c0b67e1041b7a3d1a5db57a66705d9dbf57454265987
|
7
|
+
data.tar.gz: 9f6a5c6304471df7a2b8f2cbb6b584c6f1bdcf425d94e0904bec630e45163f60a42eca8b4865ed1b0b87f7681edaf83eeac6181ce752863f673e4185f1666d48
|
data/CHANGELOG.txt
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tokn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Sember
|
@@ -35,7 +35,6 @@ files:
|
|
35
35
|
- bin/toknprocess
|
36
36
|
- CHANGELOG.txt
|
37
37
|
- README.txt
|
38
|
-
- README2.txt
|
39
38
|
- test/Example1.rb
|
40
39
|
- test/data/compileddfa.txt
|
41
40
|
- test/data/sampletext.txt
|
data/README2.txt
DELETED
@@ -1,219 +0,0 @@
|
|
1
|
-
# @markup rdoc
|
2
|
-
|
3
|
-
== 'tokn' : A ruby gem for constructing DFAs and using them to tokenize text files.
|
4
|
-
|
5
|
-
Written and (c) by Jeff Sember, March 2013.
|
6
|
-
---
|
7
|
-
|
8
|
-
|
9
|
-
= Description of the problem
|
10
|
-
|
11
|
-
For a simple example, suppose a particular text file is designed to have
|
12
|
-
tokens of the following three types:
|
13
|
-
|
14
|
-
[] 'a' followed by any number of 'a' or 'b'
|
15
|
-
[] 'b' followed by either 'aa' or zero or more 'b'
|
16
|
-
[] 'bbb'
|
17
|
-
|
18
|
-
We will also allow an additional token, one or more spaces, to separate them.
|
19
|
-
These four token types can be written using regular expressions as:
|
20
|
-
|
21
|
-
sep: \s
|
22
|
-
tku: a(a|b)*
|
23
|
-
tkv: b(aa|b*)
|
24
|
-
tkw: bbb
|
25
|
-
|
26
|
-
We've given each token definition a name (to the left of the colon).
|
27
|
-
|
28
|
-
Now suppose your program needs to read a text file and interpret the tokens it
|
29
|
-
finds there. {This can be done using the DFA (deterministic finite state automaton)
|
30
|
-
shown here.}[link:sample_dfa.pdf]
|
31
|
-
|
32
|
-
The token extraction algorithm has these steps:
|
33
|
-
|
34
|
-
1. Begin at the start state, S0.
|
35
|
-
1. Look at the next character in the source (text) file. If there is an arrow (edge) labelled with that character, follow it to another state (it may lead to the same state; that's okay), and advance the cursor to the next character in the source file.
|
36
|
-
1. If there's an arrow labelled with a negative number N, don't follow the edge, but instead remember the lowest (i.e., most negative) such N found.
|
37
|
-
1. Continue steps 2 and 3 until no further progress is possible.
|
38
|
-
1. At this point, N indicates the name of the token found. The cursor should be restored to the point it was at when that N was recorded. The token's text consists of the characters from the starting cursor position to that point.
|
39
|
-
1. If no N value was recorded, then the source text doesn't match any of the tokens, which is considered an error.
|
40
|
-
|
41
|
-
|
42
|
-
The tokn module provides a simple and efficient way to perform this tokenization process.
|
43
|
-
Its major accomplishment is not just performing the above six steps, but rather that
|
44
|
-
it also can construct, from a set of token definitions, the DFA to be used in these steps.
|
45
|
-
Such DFAs are very useful, and can be used by non-Ruby programs as well.
|
46
|
-
|
47
|
-
|
48
|
-
= Using the tokn module in a Ruby program
|
49
|
-
|
50
|
-
There are three object classes of interest: DFA, Tokenizer, and Token. A DFA is
|
51
|
-
compiled once from a script containing token definitions (e.g, "tku: b(aa|b*) ..."),
|
52
|
-
and can then be stored (either in memory, or on disk as a JSON string) for later use.
|
53
|
-
|
54
|
-
When tokens need to be extracted from a source file (or simple string), a Tokenizer is
|
55
|
-
constructed. It requires both the DFA and the source file as input. Once this is done,
|
56
|
-
individual Token objects can be read from the Tokenizer.
|
57
|
-
|
58
|
-
Here's some example Ruby code showing how a text file "source.txt" can be split into
|
59
|
-
tokens. We'll assume there's a text file "tokendefs.txt" that contains the
|
60
|
-
definitions shown earlier.
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
require "Tokenizer"
|
69
|
-
|
70
|
-
include Tokn
|
71
|
-
|
72
|
-
dfa = DFA.from_script(readTextFile("tokendefs.txt"))
|
73
|
-
|
74
|
-
t = Tokenizer.new(dfa, readTextFile("source.txt"))
|
75
|
-
|
76
|
-
while t.hasNext
|
77
|
-
|
78
|
-
k = t.read
|
79
|
-
|
80
|
-
if t.typeOf(k) == "sep"
|
81
|
-
next
|
82
|
-
end
|
83
|
-
|
84
|
-
...do something with the token ...
|
85
|
-
end
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
lorum epson:
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
require "Tokenizer"
|
97
|
-
|
98
|
-
include Tokn # Avoids having to prefix things with 'Tokn::'
|
99
|
-
|
100
|
-
dfa = DFA.from_script(readTextFile("tokendefs.txt"))
|
101
|
-
|
102
|
-
t = Tokenizer.new(dfa, readTextFile("source.txt"))
|
103
|
-
|
104
|
-
while t.hasNext
|
105
|
-
|
106
|
-
k = t.read # read token
|
107
|
-
|
108
|
-
if t.typeOf(k) == "sep" # skip 'whitespace'
|
109
|
-
next
|
110
|
-
end
|
111
|
-
|
112
|
-
...do something with the token ...
|
113
|
-
end
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
If later, another file needs to be tokenized, a new Tokenizer object can be
|
120
|
-
constructed and given the same dfa object as earlier.
|
121
|
-
|
122
|
-
|
123
|
-
= Using the tokn command line utilities
|
124
|
-
|
125
|
-
The module has two utility scripts: tokncompile, and toknprocess. These can be
|
126
|
-
found in the bin/ directory.
|
127
|
-
|
128
|
-
The tokncompile script reads a token definition script from standard input, and
|
129
|
-
compiles it to a DFA. For example, if you are in the tokn/test/data directory, you can
|
130
|
-
type:
|
131
|
-
|
132
|
-
tokncompile < sampletokens.txt > compileddfa.txt
|
133
|
-
|
134
|
-
It will produce the JSON encoding of the appropriate DFA. For a description of how
|
135
|
-
this JSON string represents the DFA, see Dfa.rb.
|
136
|
-
|
137
|
-
The toknprocess script takes two arguments: the name of a file containing a
|
138
|
-
previously compiled DFA, and the name of a source file. It extracts the sequence
|
139
|
-
of tokens from the source file to the standard output:
|
140
|
-
|
141
|
-
toknprocess compileddfa.txt sampletext.txt
|
142
|
-
|
143
|
-
This will produce the following output:
|
144
|
-
|
145
|
-
WS 1 1 // Example source file that can be tokenized
|
146
|
-
|
147
|
-
WS 2 1
|
148
|
-
|
149
|
-
ID 3 1 speed
|
150
|
-
WS 3 6
|
151
|
-
ASSIGN 3 7 =
|
152
|
-
WS 3 8
|
153
|
-
INT 3 9 42
|
154
|
-
WS 3 11
|
155
|
-
WS 3 14 // speed of object
|
156
|
-
|
157
|
-
WS 4 1
|
158
|
-
|
159
|
-
ID 5 1 gravity
|
160
|
-
WS 5 8
|
161
|
-
ASSIGN 5 9 =
|
162
|
-
WS 5 10
|
163
|
-
DBL 5 11 -9.80
|
164
|
-
WS 5 16
|
165
|
-
|
166
|
-
|
167
|
-
ID 7 1 title
|
168
|
-
WS 7 6
|
169
|
-
ASSIGN 7 7 =
|
170
|
-
WS 7 8
|
171
|
-
LBL 7 9 'This is a string with \' an escaped delimiter'
|
172
|
-
WS 7 56
|
173
|
-
|
174
|
-
|
175
|
-
IF 9 1 if
|
176
|
-
WS 9 3
|
177
|
-
ID 9 4 gravity
|
178
|
-
WS 9 11
|
179
|
-
EQUIV 9 12 ==
|
180
|
-
WS 9 14
|
181
|
-
INT 9 15 12
|
182
|
-
WS 9 17
|
183
|
-
BROP 9 18 {
|
184
|
-
WS 9 19
|
185
|
-
|
186
|
-
DO 10 3 do
|
187
|
-
WS 10 5
|
188
|
-
ID 10 6 something
|
189
|
-
WS 10 15
|
190
|
-
|
191
|
-
BRCL 11 1 }
|
192
|
-
WS 11 2
|
193
|
-
|
194
|
-
The extra linefeeds are the result of a token containing a linefeed.
|
195
|
-
|
196
|
-
|
197
|
-
= FAQ
|
198
|
-
|
199
|
-
1. Why can't I just use Ruby's regular expressions for tokenizing text?
|
200
|
-
|
201
|
-
You could construct a regular expression describing each possible token, and use that
|
202
|
-
to extract a token from the start of a string; you could then remove that token from the
|
203
|
-
string, and repeat. The trouble is that the regular expression has no easy way to indicate
|
204
|
-
which individual token's expression was matched. You would then (presumably) have to match
|
205
|
-
the returned token with each individual regular expression to identify the token type.
|
206
|
-
|
207
|
-
Another reason why standard regular expressions can be troublesome is that their
|
208
|
-
implementations actually 'recognize' a richer class of tokens than the ones described
|
209
|
-
here. This extra power can come at a cost; in some pathological cases, the running time
|
210
|
-
can become exponential.
|
211
|
-
|
212
|
-
1. Is tokn compatible with Unicode?
|
213
|
-
|
214
|
-
The tokn tool is capable of extracting tokens made up of characters that have
|
215
|
-
codes in the entire Unicode range: 0 through 0x10ffff (hex). In fact, the labels
|
216
|
-
on the DFA edges can be viewed as sets of any nonnegative integers (negative
|
217
|
-
values are reserved for the token identifiers). Note however that the current implementation
|
218
|
-
only reads Ruby characters from the input, which I believe are only 8 bits wide.
|
219
|
-
|