-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTurtle.js
177 lines (138 loc) · 6.69 KB
/
Turtle.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var Tokenizer = function (source) {
this.source = source;
this.position = 0; // current position (or current peekposition if peeking)
this.peekPosition = []; // for storing current position when peeking
this.indents = []; // for indents - significant whitespace
this.endOfScript = function () {
return this.position >= source.length;
};
// returns next char and advances position
this.nextChar = function () {
var result = this.currentChar();
this.next();
return result;
};
// advances position
this.next = function () {
this.position += 1;
};
// returns current char
this.currentChar = function () {
if (this.position >= this.source.length) {
return "";
}
return this.source.substring(this.position, this.position + 1);
};
// returns if peeking is active
this.peeking = function () {
return this.peekPosition.length !== 0;
};
// push/start peeking
this.peek = function () {
this.peekPosition.push(this.position);
};
// pop/stop peeking
this.unPeek = function () {
if (this.peeking()) {
this.position = this.peekPosition.pop();
}
};
// checks for next token to be s and advances position
this.nextIs = function (s, caseSensitive) {
caseSensitive = (caseSensitive === undefined) ? true : caseSensitive;
if (this.peekNextIs(s, caseSensitive)) {
this.position += s.length;
return true;
}
return false;
};
// checks for next token to be s
this.peekNextIs = function (s, caseSensitive) {
caseSensitive = (caseSensitive === undefined) ? true : caseSensitive;
if (this.position + s.length > this.source.length) {
return false;
}
var match = this.source.substring(this.position, this.position + s.length);
if (caseSensitive) {
return match === s;
}
return match.toLowerCase() === s.toLowerCase();
};
// inserts a string at mark
this.insertFrom = function (mark, stringToInsert) {
this.source = source.substring(0, mark) + stringToInsert + "
" + source.substring(this.position);
this.position = mark;
};
};
var tokenizer = new Tokenizer(source + "
");
this.alpha_char = function () {"A".."Z""a".."z"}this.numeric_char = function () {"0".."9"}this.hexadecimal_char = function () {numeric_char"A".."F""a".."f"}this.alphanumeric_char = function () {numeric_charalpha_char}this.any_char = function () {"\\\"""\\\'""\\t""\\r""\\n""\\\\""#".."+"" " | "!""(".."\0xFFFF""\\0x" hexadecimal_char"\\0x" hexadecimal_char hexadecimal_char hexadecimal_char hexadecimal_charnewlinewhitespace}this.whitespace = function () {" " | "\t" { " " | "\t" }}this.optwhitespace = function () {[ whitespace ]}this.newline = function () {"\r" | "\n" | ";" { " " | "\t" | "\r" | "\n" | ";" }}this.optnewline = function () {[ newline ]}this.identifier = function () {alpha_char { alphanumeric_char | "_"}}this.newline = function () {"\r\n""\n\r""\r""\n"}this.newlines = function () {optwhitespace newline { optwhitespace newline }}this.entry = function () {identifier ":" newlines { definition newlines } -> "this." identifier " = function () {" definition "}"identifier optwhitespace "=" definition newlines -> "this." identifier " = function () {" definition "}"}this.literal = function () {"\"" { any_char } "\"""\'" { any_char } "\'"}this.range = function () {literal optwhitespace ".." literal}this.output_literal = function () {"\"" { any_char } "\"""\'" { any_char } "\'"}this.definition = function () {expression [ optwhitespace "->" optwhitespace output]}this.item = function () {rangeliteralidentifier"{" expression optwhitespace "}""[" expression optwhitespace "]""(" expression optwhitespace ")"}this.or_expression = function () {optwhitespace item { optwhitespace "|" or_expression }}this.expression = function () {optwhitespace or_expression { optwhitespace or_expression }}this.output_item = function () {"#indent" | "#block"output_literalidentifier}this.output = function () {output_item { whitespace output_item }}this.body = function () {{ "#include" {whitespace} literal }{ entry }}this.script = function () {{ body } -> '
var Tokenizer = function (source) {
this.source = source;
this.position = 0; // current position (or current peekposition if peeking)
this.peekPosition = []; // for storing current position when peeking
this.indents = []; // for indents - significant whitespace
this.endOfScript = function () {
return this.position >= source.length;
};
// returns next char and advances position
this.nextChar = function () {
var result = this.currentChar();
this.next();
return result;
};
// advances position
this.next = function () {
this.position += 1;
};
// returns current char
this.currentChar = function () {
if (this.position >= this.source.length) {
return \"\";
}
return this.source.substring(this.position, this.position + 1);
};
// returns if peeking is active
this.peeking = function () {
return this.peekPosition.length !== 0;
};
// push/start peeking
this.peek = function () {
this.peekPosition.push(this.position);
};
// pop/stop peeking
this.unPeek = function () {
if (this.peeking()) {
this.position = this.peekPosition.pop();
}
};
// checks for next token to be s and advances position
this.nextIs = function (s, caseSensitive) {
caseSensitive = (caseSensitive === undefined) ? true : caseSensitive;
if (this.peekNextIs(s, caseSensitive)) {
this.position += s.length;
return true;
}
return false;
};
// checks for next token to be s
this.peekNextIs = function (s, caseSensitive) {
caseSensitive = (caseSensitive === undefined) ? true : caseSensitive;
if (this.position + s.length > this.source.length) {
return false;
}
var match = this.source.substring(this.position, this.position + s.length);
if (caseSensitive) {
return match === s;
}
return match.toLowerCase() === s.toLowerCase();
};
// inserts a string at mark
this.insertFrom = function (mark, stringToInsert) {
this.source = source.substring(0, mark) + stringToInsert + \"\n\" + source.substring(this.position);
this.position = mark;
};
};
var tokenizer = new Tokenizer(source + \"\n\");
' body}