]> gitweb.factorcode.org Git - factor.git/blob - core/parser/parser-docs.factor
parsed -> suffix!, add append!
[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:"
10 { $subsections
11     scan
12     scan-word
13 }
14 "For example, the " { $link POSTPONE: HEX: } " word uses this feature to read hexadecimal literals:"
15 { $see POSTPONE: HEX: }
16 "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:"
17 { $see parse-base }
18 "Another simple example is the " { $link POSTPONE: \ } " word:"
19 { $see POSTPONE: \ } ;
20
21 ARTICLE: "parsing-word-nest" "Nested structure"
22 "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."
23 $nl
24 "A simple example is the parsing word that reads a quotation:"
25 { $see POSTPONE: [ }
26 "This word uses a utility word which recursively invokes the parser, reading objects into a new accumulator until an occurrence of " { $link POSTPONE: ] } ":"
27 { $subsections parse-literal }
28 "There is another, lower-level word for reading nested structure, which is also useful when called directly:"
29 { $subsections parse-until }
30 "Words such as " { $link POSTPONE: ] } " use a declaration which causes them to throw an error when an unpaired occurrence is encountered:"
31 { $subsections POSTPONE: delimiter }
32 { $see-also POSTPONE: { POSTPONE: H{ POSTPONE: V{ POSTPONE: W{ POSTPONE: T{ POSTPONE: } } ;
33
34 ARTICLE: "defining-words" "Defining words"
35 "Defining words add definitions to the dictionary without modifying the parse tree. The simplest example is the " { $link POSTPONE: SYMBOL: } " word."
36 { $see POSTPONE: SYMBOL: }
37 "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 } "."
38 { $subsections
39     CREATE
40     CREATE-WORD
41 }
42 "Colon definitions are defined in a more elaborate way:"
43 { $subsections POSTPONE: : }
44 "The " { $link POSTPONE: : } " word first calls " { $link CREATE } ", and then reads input until reaching " { $link POSTPONE: ; } " using a utility word:"
45 { $subsections parse-definition }
46 "The " { $link POSTPONE: ; } " word is just a delimiter; an unpaired occurrence throws a parse error:"
47 { $see POSTPONE: ; }
48 "There are additional parsing words whose syntax is delimited by " { $link POSTPONE: ; } ", and they are all implemented by calling " { $link parse-definition } "." ;
49
50 ARTICLE: "parsing-tokens" "Parsing raw tokens"
51 "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."
52 $nl
53 "One example is the " { $link POSTPONE: USING: } " parsing word."
54 { $see POSTPONE: USING: } 
55 "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:"
56 { $subsections parse-tokens } ;
57
58 ARTICLE: "parsing-words" "Parsing words"
59 "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."
60 $nl
61 "Parsing words are defined using the defining word:"
62 { $subsections POSTPONE: SYNTAX: }
63 "Parsing words have uppercase names by convention. Here is the simplest possible parsing word; it prints a greeting at parse time:"
64 { $code "SYNTAX: HELLO \"Hello world\" print ;" }
65 "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."
66 $nl
67 "Parsing words can read input, add word definitions to the dictionary, and do anything an ordinary word can."
68 $nl
69 "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."
70 $nl
71 "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:"
72 { $subsections staging-violation }
73 "Tools for implementing parsing words:"
74 { $subsections
75     "reading-ahead"
76     "parsing-word-nest"
77     "defining-words"
78     "parsing-tokens"
79     "word-search-parsing"
80 } ;
81
82 ARTICLE: "parser-files" "Parsing source files"
83 "The parser can run source files:"
84 { $subsections
85     run-file
86     parse-file
87 }
88 "The parser cross-references source files and definitions. This allows it to keep track of removed definitions, and prevent forward references and accidental redefinitions."
89 $nl
90 "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" } "."
91 { $see-also "source-files" } ;
92
93 ARTICLE: "top-level-forms" "Top level forms"
94 "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."
95 $nl
96 "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."
97 $nl
98 "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." ;
99
100 ARTICLE: "parser" "The parser"
101 "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."
102 $nl
103 "This section concerns itself with usage and extension of the parser. Standard syntax is described in " { $link "syntax" } "."
104 { $subsections "parser-files" }
105 "The parser can be extended."
106 { $subsections "parser-lexer" }
107 "The parser can be invoked reflectively;"
108 { $subsections parse-stream }
109 { $see-also "parsing-words" "definitions" "definition-checking" } ;
110
111 ABOUT: "parser"
112
113 HELP: location
114 { $values { "loc" "a " { $snippet "{ path line# }" } " pair" } }
115 { $description "Outputs the current parser location. This value can be passed to " { $link set-where } " or " { $link remember-definition } "." } ;
116
117 HELP: save-location
118 { $values { "definition" "a definition specifier" } }
119 { $description "Saves the location of a definition and associates this definition with the current source file." } ;
120
121 HELP: bad-number
122 { $error-description "Indicates the parser encountered an invalid numeric literal." } ;
123
124 HELP: create-in
125 { $values { "str" "a word name" } { "word" "a new word" } }
126 { $description "Creates a word in the current vocabulary. Until re-defined, the word throws an error when invoked." }
127 $parsing-note ;
128
129 HELP: CREATE
130 { $values { "word" word } }
131 { $description "Reads the next token from the line currently being parsed, and creates a word with that name in the current vocabulary." }
132 { $errors "Throws an error if the end of the line is reached." }
133 $parsing-note ;
134
135 HELP: no-word-error
136 { $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." }
137 { $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: } "." } ;
138
139 HELP: no-word
140 { $values { "name" string } { "newword" word } }
141 { $description "Throws a " { $link no-word-error } "." } ;
142
143 HELP: parse-word
144 { $values { "string" string } { "word/number" "a word or number" } }
145 { $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." }
146 { $errors "Throws an error if the token does not name a word, and does not parse as a number." }
147 { $notes "This word is used to implement " { $link scan-word } "." } ;
148
149 HELP: scan-word
150 { $values { "word/number/f" "a word, number or " { $link f } } }
151 { $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. Outputs " { $link f } " if the end of the input has been reached." }
152 { $errors "Throws an error if the token does not name a word, and does not parse as a number." }
153 $parsing-note ;
154
155 { scan-word parse-word } related-words
156
157 HELP: parse-step
158 { $values { "accum" vector } { "end" word } { "?" "a boolean" } }
159 { $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." }
160 $parsing-note ;
161
162 HELP: (parse-until)
163 { $values { "accum" vector } { "end" word } }
164 { $description "Parses objects from parser input until " { $snippet "end" } " is encountered, adding them to the accumulator." }
165 $parsing-note ;
166
167 HELP: parse-until
168 { $values { "end" word } { "vec" "a new vector" } }
169 { $description "Parses objects from parser input until " { $snippet "end" } ". Outputs a new vector with the results." }
170 { $examples "This word is used to implement " { $link POSTPONE: ARTICLE: } "." }
171 $parsing-note ;
172
173 { parse-tokens (parse-until) parse-until } related-words
174
175 HELP: (parse-lines)
176 { $values { "lexer" lexer } { "quot" "a new " { $link quotation } } }
177 { $description "Parses Factor source code using a custom lexer. The vocabulary search path is taken from the current scope." }
178 { $errors "Throws a " { $link lexer-error } " if the input is malformed." } ;
179
180 HELP: parse-lines
181 { $values { "lines" "a sequence of strings" } { "quot" "a new " { $link quotation } } }
182 { $description "Parses Factor source code which has been tokenized into lines. The vocabulary search path is taken from the current scope." }
183 { $errors "Throws a " { $link lexer-error } " if the input is malformed." } ;
184
185 HELP: parse-base
186 { $values { "parsed" integer } { "base" "an integer between 2 and 36" } { "parsed" integer } }
187 { $description "Reads an integer in a specific numerical base from the parser input." }
188 $parsing-note ;
189
190 HELP: parse-literal
191 { $values { "accum" vector } { "end" word } { "quot" { $quotation "( seq -- obj )" } } }
192 { $description "Parses objects from parser input until " { $snippet "end" } ", applies the quotation to the resulting sequence, and adds the output value to the accumulator." }
193 { $examples "This word is used to implement " { $link POSTPONE: [ } "." }
194 $parsing-note ;
195
196 HELP: parse-definition
197 { $values { "quot" "a new " { $link quotation } } }
198 { $description "Parses objects from parser input until " { $link POSTPONE: ; } " and outputs a quotation with the results." }
199 { $examples "This word is used to implement " { $link POSTPONE: : } "." }
200 $parsing-note ;
201
202 HELP: bootstrap-syntax
203 { $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." } ;
204
205 HELP: with-file-vocabs
206 { $values { "quot" quotation } }
207 { $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." } ;
208
209 HELP: parse-fresh
210 { $values { "lines" "a sequence of strings" } { "quot" quotation } }
211 { $description "Parses Factor source code in a sequence of lines. The initial vocabulary search path is used (see " { $link with-file-vocabs } ")." }
212 { $errors "Throws a parse error if the input is malformed." } ;
213
214 HELP: filter-moved
215 { $values { "assoc1" assoc } { "assoc2" assoc } { "seq" "an seqence of definitions" } }
216 { $description "Removes all definitions from " { $snippet "assoc2" } " which are in " { $snippet "assoc1" } " or are are no longer present in the current " { $link file } "." } ;
217
218 HELP: forget-smudged
219 { $description "Forgets removed definitions and prints a warning message if any of them are still referenced from other source files." } ;
220
221 HELP: finish-parsing
222 { $values { "lines" "the lines of text just parsed" } { "quot" "the quotation just parsed" } }
223 { $description "Records information to the current " { $link file } "." }
224 { $notes "This is one of the factors of " { $link parse-stream } "." } ;
225
226 HELP: parse-stream
227 { $values { "stream" "an input stream" } { "name" "a file name for error reporting and cross-referencing" } { "quot" quotation } }
228 { $description "Parses Factor source code read from the stream. The initial vocabulary search path is used." }
229 { $errors "Throws an I/O error if there was an error reading from the stream. Throws a parse error if the input is malformed." } ;
230
231 HELP: parse-file
232 { $values { "file" "a pathname string" } { "quot" quotation } }
233 { $description "Parses the Factor source code stored in a file. The initial vocabulary search path is used." }
234 { $errors "Throws an I/O error if there was an error reading from the file. Throws a parse error if the input is malformed." } ;
235
236 HELP: run-file
237 { $values { "file" "a pathname string" } }
238 { $description "Parses the Factor source code stored in a file and runs it. The initial vocabulary search path is used." }
239 { $errors "Throws an error if loading the file fails, there input is malformed, or if a runtime error occurs while calling the parsed quotation." }  ;
240
241 HELP: ?run-file
242 { $values { "path" "a pathname string" } }
243 { $description "If the file exists, runs it with " { $link run-file } ", otherwise does nothing." } ;
244
245 HELP: staging-violation
246 { $values { "word" word } }
247 { $description "Throws a " { $link staging-violation } " error." }
248 { $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" } "." }
249 { $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." } ;
250
251 HELP: auto-use?
252 { $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." }
253 { $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" } "." } ;