]> gitweb.factorcode.org Git - factor.git/blob - core/parser/parser-docs.factor
d25a647aa34800817992581e922bab0480a61471
[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 namespaces compiler.units assocs lexer
5 words.symbol words.alias words.constant vocabs.parser ;
6 IN: parser
7
8 ARTICLE: "reading-ahead" "Reading ahead"
9 "Parsing words can consume input from the input stream. Words come in two flavors: words that throw upon finding end of file, and words that return " { $link f } " upon the same." $nl
10 "Parsing words that throw on end of file:"
11 { $subsections
12     scan-token
13     scan-word-name
14     scan-word
15     scan-datum
16     scan-number
17     scan-object
18 }
19 "Parsing words that return " { $link f } " on end of file:"
20 { $subsections
21     (scan-token)
22     (scan-datum)
23 }
24 "A simple example is the " { $link POSTPONE: \ } " word:"
25 { $see POSTPONE: \ } ;
26
27 ARTICLE: "parsing-word-nest" "Nested structure"
28 "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."
29 $nl
30 "A simple example is the parsing word that reads a quotation:"
31 { $see POSTPONE: [ }
32 "This word uses a utility word which recursively invokes the parser, reading objects into a new accumulator until an occurrence of " { $link POSTPONE: ] } ":"
33 { $subsections parse-literal }
34 "There is another, lower-level word for reading nested structure, which is also useful when called directly:"
35 { $subsections parse-until }
36 "Words such as " { $link POSTPONE: ] } " use a declaration which causes them to throw an error when an unpaired occurrence is encountered:"
37 { $subsections POSTPONE: delimiter }
38 { $see-also POSTPONE: { POSTPONE: H{ POSTPONE: V{ POSTPONE: W{ POSTPONE: T{ POSTPONE: } } ;
39
40 ARTICLE: "defining-words" "Defining words"
41 "Defining words add definitions to the dictionary without modifying the parse tree. The simplest example is the " { $link POSTPONE: SYMBOL: } " word."
42 { $see POSTPONE: SYMBOL: }
43 "The key factor in the definition of " { $link POSTPONE: SYMBOL: } " is " { $link scan-new } ", which reads a token from the input and creates a word with that name. This word is then passed to " { $link define-symbol } "."
44 { $subsections
45     scan-new
46     scan-new-word
47 }
48 "Colon definitions are defined in a more elaborate way:"
49 { $subsections POSTPONE: : }
50 "The " { $link POSTPONE: : } " word first calls " { $link scan-new } ", and then reads input until reaching " { $link POSTPONE: ; } " using a utility word:"
51 { $subsections parse-definition }
52 "The " { $link POSTPONE: ; } " word is just a delimiter; an unpaired occurrence throws a parse error:"
53 { $see POSTPONE: ; }
54 "There are additional parsing words whose syntax is delimited by " { $link POSTPONE: ; } ", and they are all implemented by calling " { $link parse-definition } "." ;
55
56 ARTICLE: "parsing-tokens" "Parsing raw tokens"
57 "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."
58 $nl
59 "One example is the " { $link POSTPONE: USING: } " parsing word."
60 { $see POSTPONE: USING: }
61 "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 set of lower-level combinators can be used:"
62 { $subsections
63     each-token
64     map-tokens
65     parse-tokens
66 } ;
67
68 ARTICLE: "parsing-words" "Parsing words"
69 "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."
70 $nl
71 "Parsing words are defined using the defining word:"
72 { $subsections POSTPONE: SYNTAX: }
73 "Parsing words have uppercase names by convention. Here is the simplest possible parsing word; it prints a greeting at parse time:"
74 { $code "SYNTAX: HELLO \"Hello world\" print ;" }
75 "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."
76 $nl
77 "Parsing words can read input, add word definitions to the dictionary, and do anything an ordinary word can."
78 $nl
79 "Because of the stack restriction, parsing words cannot pass data to other words by leaving values on the stack; instead, use " { $link suffix! } " to add the data to the parse tree so that it can be evaluated later."
80 $nl
81 "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:"
82 { $subsections staging-violation }
83 "Tools for implementing parsing words:"
84 { $subsections
85     "reading-ahead"
86     "parsing-word-nest"
87     "defining-words"
88     "parsing-tokens"
89     "word-search-parsing"
90 } ;
91
92 ARTICLE: "top-level-forms" "Top level forms"
93 "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."
94 $nl
95 "Top-level forms cannot access the parse-time manifest (" { $link "word-search-parsing" } "), nor do they run inside " { $link with-compilation-unit } "; as a result, meta-programming might require extra work in a top-level form compared with a parsing word."
96 $nl
97 "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." ;
98
99 ARTICLE: "parser" "The parser"
100 "The Factor parser reads textual representations of objects and definitions, with all syntax determined by " { $link "parsing-words" } ". The parser is implemented in the " { $vocab-link "parser" } " vocabulary, with standard syntax in the " { $vocab-link "syntax" } " vocabulary. See " { $link "syntax" } " for a description of standard syntax."
101 $nl
102 "The parser cross-references " { $link "source-files" } " and " { $link "definitions" } ". This functionality is used for improved error checking, as well as tools such as " { $link "tools.crossref" } " and " { $link "editor" } "."
103 $nl
104 "The parser can be invoked reflectively, to run strings and source files."
105 { $subsections
106     "eval"
107     run-file
108     parse-file
109 }
110 "If Factor is run from the command line with a script file supplied as an argument, the script is run using " { $link run-file } ". See " { $link "command-line" } "."
111 $nl
112 "While " { $link run-file } " can be used interactively in the listener to load user code into the session, this should only be done for quick one-off scripts, and real programs should instead rely on the automatic " { $link "vocabs.loader" } "."
113 { $see-also "parsing-words" "definitions" "definition-checking" } ;
114
115 ABOUT: "parser"
116
117 HELP: location
118 { $values { "loc" "a " { $snippet "{ path line# }" } " pair" } }
119 { $description "Outputs the current parser location. This value can be passed to " { $link set-where } " or " { $link remember-definition } "." } ;
120
121 HELP: save-location
122 { $values { "definition" "a definition specifier" } }
123 { $description "Saves the location of a definition and associates this definition with the current source file." } ;
124
125 HELP: bad-number
126 { $error-description "Indicates the parser encountered an invalid numeric literal." } ;
127
128 HELP: create-in
129 { $values { "str" "a word name" } { "word" "a new word" } }
130 { $description "Creates a word in the current vocabulary. Until re-defined, the word throws an error when invoked." }
131 $parsing-note ;
132
133 HELP: scan-new
134 { $values { "word" word } }
135 { $description "Reads the next token from the parser input, and creates a word with that name in the current vocabulary." }
136 { $errors "Throws an error if the end of the file is reached." }
137 $parsing-note ;
138
139 HELP: scan-new-word
140 { $values { "word" word } }
141 { $description "Reads the next token from the parser input, and creates a word with that name in the current vocabulary and resets the generic word properties of that word." }
142 { $errors "Throws an error if the end of the file is reached." }
143 $parsing-note ;
144
145 HELP: no-word-error
146 { $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." }
147 { $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: } "." } ;
148
149 HELP: no-word
150 { $values { "name" string } { "newword" word } }
151 { $description "Throws a " { $link no-word-error } "." } ;
152
153 HELP: parse-word
154 { $values { "string" string } { "word" "a number" } }
155 { $description "If " { $snippet "string" } " is a valid number literal, it is converted to a number, otherwise the current vocabulary search path is searched for a word named by the string." }
156 { $errors "Throws an error if the token does not name a word, and does not parse as a number." }
157 { $notes "This word is used to implement " { $link scan-word } "." } ;
158
159 HELP: parse-datum
160 { $values { "string" string } { "word/number" "a word or number" } }
161 { $description "If " { $snippet "string" } " is a valid number literal, it is converted to a number, otherwise the current vocabulary search path is searched for a word named by the string." }
162 { $errors "Throws an error if the token does not name a word, and does not parse as a number." }
163 { $notes "This word is used to implement " { $link (scan-datum) } " and " { $link scan-datum } "." } ;
164
165 HELP: scan-word
166 { $values { "word" "a word" } }
167 { $description "Reads the next token from parser input. If the token is a valid number literal, it is converted to a number, otherwise the vocabulary search path is searched for a word named by the token." }
168 { $errors "Throws an error if the token does not name a word or end of file is reached." }
169 $parsing-note ;
170
171 { scan-word parse-word } related-words
172
173 HELP: scan-word-name
174 { $values { "string" string } }
175 { $description "Reads the next token from parser input and makes sure it does not parse as a number." }
176 { $errors "Throws an error if the scanned token is a number or upon finding end of file." }
177 $parsing-note ;
178
179 HELP: (scan-datum)
180 { $values { "word/number/f" "a word, a number, or " { $link f } } }
181 { $description "Reads the next token from parser input. If the token is found in the vocabulary search path, returns the word named by the token. If the token does not find a word, it is next converted to a number. If this conversion fails, too, this word returns " { $link f } "." }
182 $parsing-note ;
183
184 HELP: scan-datum
185 { $values { "word/number" "a word or a number" } }
186 { $description "Reads the next token from parser input. If the token is found in the vocabulary search path, returns the word named be the token. If the token is not found in the vocabulary search path, it is converted to a number. If this conversion fails, an error is thrown." }
187 { $errors "Throws an error if the token is not a number or end of file is reached." }
188 $parsing-note ;
189
190 HELP: scan-number
191 { $values { "number" "a number" } }
192 { $description "Reads the next token from parser input. If the token is a number literal, it is converted to a number. Otherwise, it throws an error." }
193 { $errors "Throws an error if the token is not a number or end of file is reached." }
194 $parsing-note ;
195
196 HELP: parse-until-step
197 { $values { "accum" vector } { "end" word } { "?" "a boolean" } }
198 { $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." }
199 $parsing-note ;
200
201 HELP: (parse-until)
202 { $values { "accum" vector } { "end" word } }
203 { $description "Parses objects from parser input until " { $snippet "end" } " is encountered, adding them to the accumulator." }
204 $parsing-note ;
205
206 HELP: parse-until
207 { $values { "end" word } { "vec" "a new vector" } }
208 { $description "Parses objects from parser input until " { $snippet "end" } ". Outputs a new vector with the results." }
209 { $examples "This word is used to implement " { $link POSTPONE: ARTICLE: } "." }
210 $parsing-note ;
211
212 { parse-tokens each-token map-tokens (parse-until) parse-until } related-words
213
214 HELP: (parse-lines)
215 { $values { "lexer" lexer } { "quot" "a new " { $link quotation } } }
216 { $description "Parses Factor source code using a custom lexer. The vocabulary search path is taken from the current scope." }
217 { $errors "Throws a " { $link lexer-error } " if the input is malformed." } ;
218
219 HELP: parse-lines
220 { $values { "lines" "a sequence of strings" } { "quot" "a new " { $link quotation } } }
221 { $description "Parses Factor source code which has been tokenized into lines. The vocabulary search path is taken from the current scope." }
222 { $errors "Throws a " { $link lexer-error } " if the input is malformed." } ;
223
224 HELP: parse-literal
225 { $values { "accum" vector } { "end" word } { "quot" { $quotation "( seq -- obj )" } } }
226 { $description "Parses objects from parser input until " { $snippet "end" } ", applies the quotation to the resulting sequence, and adds the output value to the accumulator." }
227 { $examples "This word is used to implement " { $link POSTPONE: [ } "." }
228 $parsing-note ;
229
230 HELP: parse-definition
231 { $values { "quot" "a new " { $link quotation } } }
232 { $description "Parses objects from parser input until " { $link POSTPONE: ; } " and outputs a quotation with the results." }
233 { $examples "This word is used to implement " { $link POSTPONE: : } "." }
234 $parsing-note ;
235
236 HELP: bootstrap-syntax
237 { $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." } ;
238
239 HELP: with-file-vocabs
240 { $values { "quot" quotation } }
241 { $description "Calls the quotation in a scope with an initial vocabulary search path consisting of just the " { $snippet "syntax" } " vocabulary." } ;
242
243 HELP: parse-fresh
244 { $values { "lines" "a sequence of strings" } { "quot" quotation } }
245 { $description "Parses Factor source code in a sequence of lines. The initial vocabulary search path is used (see " { $link with-file-vocabs } ")." }
246 { $errors "Throws a parse error if the input is malformed." } ;
247
248 HELP: filter-moved
249 { $values { "set1" set } { "set2" set } { "seq" "an sequence of definitions" } }
250 { $description "Removes all definitions from " { $snippet "set2" } " which are in " { $snippet "set1" } " or are no longer present in the current " { $link file } "." } ;
251
252 HELP: forget-smudged
253 { $description "Forgets removed definitions." } ;
254
255 HELP: finish-parsing
256 { $values { "lines" "the lines of text just parsed" } { "quot" "the quotation just parsed" } }
257 { $description "Records information to the current " { $link file } "." }
258 { $notes "This is one of the factors of " { $link parse-stream } "." } ;
259
260 HELP: parse-stream
261 { $values { "stream" "an input stream" } { "name" "a file name for error reporting and cross-referencing" } { "quot" quotation } }
262 { $description "Parses Factor source code read from the stream. The initial vocabulary search path is used." }
263 { $errors "Throws an I/O error if there was an error reading from the stream. Throws a parse error if the input is malformed." } ;
264
265 HELP: parse-file
266 { $values { "file" "a pathname string" } { "quot" quotation } }
267 { $description "Parses the Factor source code stored in a file. The initial vocabulary search path is used." }
268 { $errors "Throws an I/O error if there was an error reading from the file. Throws a parse error if the input is malformed." } ;
269
270 HELP: run-file
271 { $values { "file" "a pathname string" } }
272 { $description "Parses the Factor source code stored in a file and runs it. The initial vocabulary search path is used." }
273 { $errors "Throws an error if loading the file fails, there input is malformed, or if a runtime error occurs while calling the parsed quotation." }  ;
274
275 HELP: ?run-file
276 { $values { "path" "a pathname string" } }
277 { $description "If the file exists, runs it with " { $link run-file } ", otherwise does nothing." } ;
278
279 HELP: staging-violation
280 { $values { "word" word } }
281 { $description "Throws a " { $link staging-violation } " error." }
282 { $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" } "." }
283 { $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." } ;
284
285 HELP: auto-use?
286 { $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." }
287 { $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 "word-search-errors" } "." } ;
288
289 HELP: scan-object
290 { $values { "object" object } }
291 { $description "Parses a literal representation of an object." }
292 $parsing-note ;