]> gitweb.factorcode.org Git - factor.git/blob - core/parser/parser-docs.factor
92e5922802bbab824b4691a228d27f1792282303
[factor.git] / core / parser / parser-docs.factor
1 USING: help.markup help.syntax kernel sequences words
2 math strings vectors quotations generic effects classes
3 vocabs.loader definitions io vocabs source-files
4 quotations namespaces compiler.units assocs lexer ;
5 IN: parser
6
7 ARTICLE: "vocabulary-search-shadow" "Shadowing word names"
8 "If adding a vocabulary to the search path results in a word in another vocabulary becoming inaccessible due to the new vocabulary defining a word with the same name, we say that the old word has been " { $emphasis "shadowed" } "."
9 $nl
10 "Here is an example where shadowing occurs:"
11 { $code
12     "IN: foe"
13     "USING: sequences io ;"
14     ""
15     ": append"
16     "    \"foe::append calls sequences:append\" print  append ;"
17     ""
18     "IN: fee"
19     ""
20     ": append"
21     "    \"fee::append calls fee:append\" print  append ;"
22     ""
23     "IN: fox"
24     "USE: foe"
25     ""
26     ": append"
27     "    \"fox::append calls foe:append\" print  append ;"
28     ""
29     "\"1234\" \"5678\" append print"
30     ""
31     "USE: fox"
32     "\"1234\" \"5678\" append print"
33 }
34 "When placed in a source file and run, the above code produces the following output:"
35 { $code
36     "foe:append calls sequences:append"
37     "12345678"
38     "fee:append calls foe:append"
39     "foe:append calls sequences:append"
40     "12345678"
41 }
42 "The " { $vocab-link "qualified" } " vocabulary contains some tools for helping with shadowing." ;
43
44 ARTICLE: "vocabulary-search-errors"  "Word lookup errors"
45 "If the parser cannot not find a word in the current vocabulary search path, it attempts to look for the word in all loaded vocabularies."
46 $nl
47 "If " { $link auto-use? } " mode is off, a restartable error is thrown with a restart for each vocabulary in question, together with a restart which defers the word in the current vocabulary, as if " { $link POSTPONE: DEFER: } " was used."
48 $nl
49 "If " { $link auto-use? } " mode is on and only one vocabulary has a word with this name, the vocabulary is added to the search path and parsing continues."
50 $nl
51 "If any restarts were invoked, or if " { $link auto-use? } " is on, the parser will print the correct " { $link POSTPONE: USING: } " after parsing completes. This form can be copy and pasted back into the source file."
52 { $subsection auto-use? } ;
53
54 ARTICLE: "vocabulary-search" "Vocabulary search path"
55 "When the parser reads a token, it attempts to look up a word named by that token. The lookup is performed by searching each vocabulary in the search path, in order."
56 $nl
57 "For a source file the vocabulary search path starts off with one vocabulary:"
58 { $code "syntax" }
59 "The " { $vocab-link "syntax" } " vocabulary consists of a set of parsing words for reading Factor data and defining new words."
60 $nl
61 "In the listener, the " { $vocab-link "scratchpad" } " is the default vocabulary for new word definitions. However, when loading source files, there is no default vocabulary. Defining words before declaring a vocabulary with " { $link POSTPONE: IN: } " results in an error."
62 $nl
63 "At the interactive listener, the default search path contains many more vocabularies. Details on the default search path and parser invocation are found in " { $link "parser" } "."
64 $nl
65 "Three parsing words deal with the vocabulary search path:"
66 { $subsection POSTPONE: USE: }
67 { $subsection POSTPONE: USING: }
68 { $subsection POSTPONE: IN: }
69 "Private words can be defined; note that this is just a convention and they can be called from other vocabularies anyway:"
70 { $subsection POSTPONE: <PRIVATE }
71 { $subsection POSTPONE: PRIVATE> }
72 { $subsection "vocabulary-search-errors" }
73 { $subsection "vocabulary-search-shadow" }
74 { $see-also "words" "qualified" } ;
75
76 ARTICLE: "reading-ahead" "Reading ahead"
77 "Parsing words can consume input:"
78 { $subsection scan }
79 { $subsection scan-word }
80 "For example, the " { $link POSTPONE: HEX: } " word uses this feature to read hexadecimal literals:"
81 { $see POSTPONE: HEX: }
82 "It is defined in terms of a lower-level word that takes the numerical base on the data stack, but reads the number from the parser and then adds it to the parse tree:"
83 { $see parse-base }
84 "Another simple example is the " { $link POSTPONE: \ } " word:"
85 { $see POSTPONE: \ } ;
86
87 ARTICLE: "parsing-word-nest" "Nested structure"
88 "Recall that the parser loop calls parsing words with an accumulator vector on the stack. The parser loop can be invoked recursively with a new, empty accumulator; the result can then be added to the original accumulator. This is how parsing words for object literals are implemented; object literals can nest arbitrarily deep."
89 $nl
90 "A simple example is the parsing word that reads a quotation:"
91 { $see POSTPONE: [ }
92 "This word uses a utility word which recursively invokes the parser, reading objects into a new accumulator until an occurrence of " { $link POSTPONE: ] } ":"
93 { $subsection parse-literal }
94 "There is another, lower-level word for reading nested structure, which is also useful when called directly:"
95 { $subsection parse-until }
96 "Words such as " { $link POSTPONE: ] } " use a declaration which causes them to throw an error when an unpaired occurrence is encountered:"
97 { $subsection POSTPONE: delimiter }
98 { $see-also POSTPONE: { POSTPONE: H{ POSTPONE: V{ POSTPONE: W{ POSTPONE: T{ POSTPONE: } } ;
99
100 ARTICLE: "defining-words" "Defining words"
101 "Defining words add definitions to the dictionary without modifying the parse tree. The simplest example is the " { $link POSTPONE: SYMBOL: } " word."
102 { $see POSTPONE: SYMBOL: }
103 "The key factor in the definition of " { $link POSTPONE: SYMBOL: } " is " { $link CREATE } ", which reads a token from the input and creates a word with that name. This word is then passed to " { $link define-symbol } "."
104 { $subsection CREATE }
105 "Colon definitions are defined in a more elaborate way:"
106 { $subsection POSTPONE: : }
107 "The " { $link POSTPONE: : } " word first calls " { $link CREATE } ", and then reads input until reaching " { $link POSTPONE: ; } " using a utility word:"
108 { $subsection parse-definition }
109 "The " { $link POSTPONE: ; } " word is just a delimiter; an unpaired occurrence throws a parse error:"
110 { $see POSTPONE: ; }
111 "There are additional parsing words whose syntax is delimited by  " { $link POSTPONE: ; } ", and they are all implemented by calling " { $link parse-definition } "." ;
112
113 ARTICLE: "parsing-tokens" "Parsing raw tokens"
114 "So far we have seen how to read individual tokens, or read a sequence of parsed objects until a delimiter. It is also possible to read raw tokens from the input and perform custom processing."
115 $nl
116 "One example is the " { $link POSTPONE: USING: } " parsing word."
117 { $see POSTPONE: USING: } 
118 "It reads a list of vocabularies terminated by " { $link POSTPONE: ; } ". However, the vocabulary names do not name words, except by coincidence; so " { $link parse-until } " cannot be used here. Instead, a lower-level word is called:"
119 { $subsection parse-tokens } ;
120
121 ARTICLE: "parsing-words" "Parsing words"
122 "The Factor parser follows a simple recursive-descent design. The parser reads successive tokens from the input; if the token identifies a number or an ordinary word, it is added to an accumulator vector. Otherwise if the token identifies a parsing word, the parsing word is executed immediately."
123 $nl
124 "Parsing words are marked by suffixing the definition with a " { $link POSTPONE: parsing } " declaration. Here is the simplest possible parsing word; it prints a greeting at parse time:"
125 { $code ": hello \"Hello world\" print ; parsing" }
126 "Parsing words must not pop or push items from the stack; however, they are permitted to access the accumulator vector supplied by the parser at the top of the stack. That is, parsing words must have stack effect " { $snippet "( accum -- accum )" } ", where " { $snippet "accum" } " is the accumulator vector supplied by the parser."
127 $nl
128 "Parsing words can read input, add word definitions to the dictionary, and do anything an ordinary word can."
129 $nl
130 "Because of the stack restriction, parsing words cannot pass data to other words by leaving values on the stack; instead, use " { $link parsed } " to add the data to the parse tree so that it can be evaluated later."
131 $nl
132 "Parsing words cannot be called from the same source file where they are defined, because new definitions are only compiled at the end of the source file. An attempt to use a parsing word in its own source file raises an error:"
133 { $subsection staging-violation }
134 "Tools for implementing parsing words:"
135 { $subsection "reading-ahead" }
136 { $subsection "parsing-word-nest" }
137 { $subsection "defining-words" }
138 { $subsection "parsing-tokens" } ;
139
140 ARTICLE: "parser-files" "Parsing source files"
141 "The parser can run source files:"
142 { $subsection run-file }
143 { $subsection parse-file }
144 "The parser cross-references source files and definitions. This allows it to keep track of removed definitions, and prevent forward references and accidental redefinitions."
145 $nl
146 "While the above words are useful for one-off experiments, real programs should be written to use the vocabulary system instead; see " { $link "vocabs.loader" } "."
147 { $see-also "source-files" } ;
148
149 ARTICLE: "top-level-forms" "Top level forms"
150 "Any code outside of a definition is known as a " { $emphasis "top-level form" } "; top-level forms are run after the entire source file has been parsed, regardless of their position in the file."
151 $nl
152 "Top-level forms do not have access to the " { $link in } " and " { $link use } " variables that were set at parse time, nor do they run inside " { $link with-compilation-unit } "; so meta-programming might require extra work in a top-level form compared with a parsing word."
153 $nl
154 "Also, top-level forms run in a new dynamic scope, so using " { $link set } " to store values is almost always wrong, since the values will be lost after the top-level form completes. To save values computed by a top-level form, either use " { $link set-global } " or define a new word with the value." ;
155
156 ARTICLE: "parser" "The parser"
157 "This parser is a general facility for reading textual representations of objects and definitions. The parser is implemented in the " { $vocab-link "parser" } " and " { $vocab-link "syntax" } " vocabularies."
158 $nl
159 "This section concerns itself with usage and extension of the parser. Standard syntax is described in " { $link "syntax" } "."
160 { $subsection "vocabulary-search" }
161 { $subsection "parser-files" }
162 { $subsection "top-level-forms" }
163 "The parser can be extended."
164 { $subsection "parsing-words" }
165 { $subsection "parser-lexer" }
166 "The parser can be invoked reflectively;"
167 { $subsection parse-stream }
168 { $see-also "definitions" "definition-checking" } ;
169
170 ABOUT: "parser"
171
172 HELP: location
173 { $values { "loc" "a " { $snippet "{ path line# }" } " pair" } }
174 { $description "Outputs the current parser location. This value can be passed to " { $link set-where } " or " { $link remember-definition } "." } ;
175
176 HELP: save-location
177 { $values { "definition" "a definition specifier" } }
178 { $description "Saves the location of a definition and associates this definition with the current source file." } ;
179
180 HELP: parser-notes
181 { $var-description "A boolean controlling whether the parser will print various notes and warnings. Switched on by default. If a source file is being run for its effect on " { $link output-stream } ", this variable should be switched off, to prevent parser notes from polluting the output." } ;
182
183 HELP: parser-notes?
184 { $values { "?" "a boolean" } }
185 { $description "Tests if the parser will print various notes and warnings. To disable parser notes, either set " { $link parser-notes } " to " { $link f } ", or pass the " { $snippet "-quiet" } " command line switch." } ;
186
187 HELP: bad-number
188 { $error-description "Indicates the parser encountered an invalid numeric literal." } ;
189
190 HELP: use
191 { $var-description "A variable holding the current vocabulary search path as a sequence of assocs." } ;
192
193 { use in use+ (use+) set-use set-in POSTPONE: USING: POSTPONE: USE: with-file-vocabs with-interactive-vocabs } related-words
194
195 HELP: in
196 { $var-description "A variable holding the name of the current vocabulary for new definitions." } ;
197
198 HELP: current-vocab
199 { $values { "str" "a vocabulary" } }
200 { $description "Returns the vocabulary stored in the " { $link in } " symbol. Throws an error if the current vocabulary is " { $link f } "." } ;
201
202 HELP: (use+)
203 { $values { "vocab" "an assoc mapping strings to words" } }
204 { $description "Adds an assoc at the front of the search path." }
205 $parsing-note ;
206
207 HELP: use+
208 { $values { "vocab" string } }
209 { $description "Adds a new vocabulary at the front of the search path after loading it if necessary. Subsequent word lookups by the parser will search this vocabulary first." }
210 $parsing-note
211 { $errors "Throws an error if the vocabulary does not exist." } ;
212
213 HELP: set-use
214 { $values { "seq" "a sequence of strings" } }
215 { $description "Sets the vocabulary search path. Later vocabularies take precedence." }
216 { $errors "Throws an error if one of the vocabularies does not exist." }
217 $parsing-note ;
218
219 HELP: add-use
220 { $values { "seq" "a sequence of strings" } }
221 { $description "Adds multiple vocabularies to the search path, with later vocabularies taking precedence." }
222 { $errors "Throws an error if one of the vocabularies does not exist." }
223 $parsing-note ;
224
225 HELP: set-in
226 { $values { "name" string } }
227 { $description "Sets the current vocabulary where new words will be defined, creating the vocabulary first if it does not exist." }
228 $parsing-note ;
229
230 HELP: create-in
231 { $values { "str" "a word name" } { "word" "a new word" } }
232 { $description "Creates a word in the current vocabulary. Until re-defined, the word throws an error when invoked." }
233 $parsing-note ;
234
235 HELP: CREATE
236 { $values { "word" word } }
237 { $description "Reads the next token from the line currently being parsed, and creates a word with that name in the current vocabulary." }
238 { $errors "Throws an error if the end of the line is reached." }
239 $parsing-note ;
240
241 HELP: no-word-error
242 { $error-description "Thrown if the parser encounters a token which does not name a word in the current vocabulary search path. If any words with this name exist in vocabularies not part of the search path, a number of restarts will offer to add those vocabularies to the search path and use the chosen word." }
243 { $notes "Apart from a missing " { $link POSTPONE: USE: } ", this error can also indicate an ordering issue. In Factor, words must be defined before they can be called. Mutual recursion can be implemented via " { $link POSTPONE: DEFER: } "." } ;
244
245 HELP: no-word
246 { $values { "name" string } { "newword" word } }
247 { $description "Throws a " { $link no-word-error } "." } ;
248
249 HELP: search
250 { $values { "str" string } { "word/f" "a word or " { $link f } } }
251 { $description "Searches for a word by name in the current vocabulary search path. If no such word could be found, outputs " { $link f } "." }
252 $parsing-note ;
253
254 HELP: scan-word
255 { $values { "word/number/f" "a word, number or " { $link f } } }
256 { $description "Reads the next token from parser input. If the token is a valid number literal, it is converted to a number, otherwise the dictionary is searched for a word named by the token. Outputs " { $link f } " if the end of the input has been reached." }
257 { $errors "Throws an error if the token does not name a word, and does not parse as a number." }
258 $parsing-note ;
259
260 HELP: parse-step
261 { $values { "accum" vector } { "end" word } { "?" "a boolean" } }
262 { $description "Parses a token. If the token is a number or an ordinary word, it is added to the accumulator. If it is a parsing word, calls the parsing word with the accumulator on the stack. Outputs " { $link f } " if " { $snippet "end" } " is encountered, " { $link t } " otherwise." }
263 $parsing-note ;
264
265 HELP: (parse-until)
266 { $values { "accum" vector } { "end" word } }
267 { $description "Parses objects from parser input until " { $snippet "end" } " is encountered, adding them to the accumulator." }
268 $parsing-note ;
269
270 HELP: parse-until
271 { $values { "end" word } { "vec" "a new vector" } }
272 { $description "Parses objects from parser input until " { $snippet "end" } ". Outputs a new vector with the results." }
273 { $examples "This word is used to implement " { $link POSTPONE: ARTICLE: } "." }
274 $parsing-note ;
275
276 { parse-tokens (parse-until) parse-until } related-words
277
278 HELP: parsed
279 { $values { "accum" vector } { "obj" object } }
280 { $description "Convenience word for parsing words. It behaves exactly the same as " { $link push } ", except the accumulator remains on the stack." }
281 $parsing-note ;
282
283 HELP: (parse-lines)
284 { $values { "lexer" lexer } { "quot" "a new " { $link quotation } } }
285 { $description "Parses Factor source code using a custom lexer. The vocabulary search path is taken from the current scope." }
286 { $errors "Throws a " { $link lexer-error } " if the input is malformed." } ;
287
288 HELP: parse-lines
289 { $values { "lines" "a sequence of strings" } { "quot" "a new " { $link quotation } } }
290 { $description "Parses Factor source code which has been tokenized into lines. The vocabulary search path is taken from the current scope." }
291 { $errors "Throws a " { $link lexer-error } " if the input is malformed." } ;
292
293 HELP: parse-base
294 { $values { "base" "an integer between 2 and 36" } { "parsed" integer } }
295 { $description "Reads an integer in a specific numerical base from the parser input." }
296 $parsing-note ;
297
298 HELP: parse-literal
299 { $values { "accum" vector } { "end" word } { "quot" { $quotation "( seq -- obj )" } } }
300 { $description "Parses objects from parser input until " { $snippet "end" } ", applies the quotation to the resulting sequence, and adds the output value to the accumulator." }
301 { $examples "This word is used to implement " { $link POSTPONE: [ } "." }
302 $parsing-note ;
303
304 HELP: parse-definition
305 { $values { "quot" "a new " { $link quotation } } }
306 { $description "Parses objects from parser input until " { $link POSTPONE: ; } " and outputs a quotation with the results." }
307 { $examples "This word is used to implement " { $link POSTPONE: : } "." }
308 $parsing-note ;
309
310 HELP: bootstrap-syntax
311 { $var-description "Only set during bootstrap. Stores a copy of the " { $link vocab-words } " of the host's syntax vocabulary; this allows the host's parsing words to be used during bootstrap source parsing, not the target's." } ;
312
313 HELP: with-file-vocabs
314 { $values { "quot" quotation } }
315 { $description "Calls the quotation in a scope with the initial the vocabulary search path for parsing a file. This consists of just the " { $snippet "syntax" } " vocabulary." } ;
316
317 HELP: parse-fresh
318 { $values { "lines" "a sequence of strings" } { "quot" quotation } }
319 { $description "Parses Factor source code in a sequence of lines. The initial vocabulary search path is used (see " { $link with-file-vocabs } ")." }
320 { $errors "Throws a parse error if the input is malformed." } ;
321
322 HELP: filter-moved
323 { $values { "assoc1" assoc } { "assoc2" assoc } { "seq" "an seqence of definitions" } }
324 { $description "Removes all definitions from " { $snippet "assoc2" } " which are in " { $snippet "assoc1" } " or are are no longer present in the current " { $link file } "." } ;
325
326 HELP: forget-smudged
327 { $description "Forgets removed definitions and prints a warning message if any of them are still referenced from other source files." } ;
328
329 HELP: finish-parsing
330 { $values { "lines" "the lines of text just parsed" } { "quot" "the quotation just parsed" } }
331 { $description "Records information to the current " { $link file } " and prints warnings about any removed definitions which are still in use." }
332 { $notes "This is one of the factors of " { $link parse-stream } "." } ;
333
334 HELP: parse-stream
335 { $values { "stream" "an input stream" } { "name" "a file name for error reporting and cross-referencing" } { "quot" quotation } }
336 { $description "Parses Factor source code read from the stream. The initial vocabulary search path is used." }
337 { $errors "Throws an I/O error if there was an error reading from the stream. Throws a parse error if the input is malformed." } ;
338
339 HELP: parse-file
340 { $values { "file" "a pathname string" } { "quot" quotation } }
341 { $description "Parses the Factor source code stored in a file. The initial vocabulary search path is used." }
342 { $errors "Throws an I/O error if there was an error reading from the file. Throws a parse error if the input is malformed." } ;
343
344 HELP: run-file
345 { $values { "file" "a pathname string" } }
346 { $description "Parses the Factor source code stored in a file and runs it. The initial vocabulary search path is used." }
347 { $errors "Throws an error if loading the file fails, there input is malformed, or if a runtime error occurs while calling the parsed quotation." }  ;
348
349 HELP: ?run-file
350 { $values { "path" "a pathname string" } }
351 { $description "If the file exists, runs it with " { $link run-file } ", otherwise does nothing." } ;
352
353 HELP: staging-violation
354 { $values { "word" word } }
355 { $description "Throws a " { $link staging-violation } " error." }
356 { $error-description "Thrown by the parser if a parsing word is used in the same compilation unit as where it was defined; see " { $link "compilation-units" } "." }
357 { $notes "One possible workaround is to use the " { $link POSTPONE: << } " word to execute code at parse time. However, executing words defined in the same source file at parse time is still prohibited." } ;
358
359 HELP: auto-use?
360 { $var-description "If set to a true value, the behavior of the parser when encountering an unknown word name is changed. If only one loaded vocabulary has a word with this name, instead of throwing an error, the parser adds the vocabulary to the search path and prints a parse note. Off by default." }
361 { $notes "This feature is intended to help during development. To generate a " { $link POSTPONE: USING: } " form automatically, enable " { $link auto-use? } ", load the source file, and copy and paste the " { $link POSTPONE: USING: } " form printed by the parser back into the file, then disable " { $link auto-use? } ". See " { $link "vocabulary-search-errors" } "." } ;