Debian Codegraph @

terps/tads/tads3/lib/gramprod.t

raw
1 #charset "us-ascii"
2
3 /*
4 * Copyright (c) 2005, 2006 Michael J. Roberts
5 *
6 * This file is part of TADS 3.
7 *
8 * This module defines some classes used by the GrammarProd intrinsic
9 * class.
10 */
11
12 #include <tads.h>
13 #include <gramprod.h>
14
15
16 /* ------------------------------------------------------------------------ */
17 /*
18 * GrammarProd descriptor classes. The GrammarProd intrinsic class's
19 * getGrammarInfo() method uses these classes to build its description of
20 * a grammar production.
21 */
22
23 /* export the classes so the intrinsic class can find them */
24 export GrammarAltInfo 'GrammarProd.GrammarAltInfo';
25 export GrammarAltTokInfo 'GrammarProd.GrammarAltTokInfo';
26
27
28 /*
29 * Rule alternative descriptor. This describes one alternative in a
30 * grammar production. An alternative is one complete list of matchable
31 * tokens.
32 *
33 * In a 'grammar' statement, alternatives are delimited by '|' symbols at
34 * the top level. Each group of tokens set off by '|' symbols is one
35 * alternative.
36 *
37 * When '|' symbols are grouped with parentheses in a 'grammar'
38 * statement, the compiler "flattens" the grouping by expanding out the
39 * parenthesized groups until it has entirely top-level alternatives.
40 * So, at the level of a GrammarProd object, there's no such thing as
41 * parentheses or nested '|' symbols.
42 */
43 class GrammarAltInfo: object
44 /*
45 * Constructor. GrammarProd.getGrammarInfo() calls this once for
46 * each alternative making up the production, passing in the values
47 * that define the alternative. Note that we have a '...' in our
48 * argument list so that we'll be compatible with any future
49 * GrammarProd versions that add additional arguments - we won't do
50 * anything with the extra arguments, but we'll harmlessly ignore
51 * them, so code compiled with this library version will continue to
52 * work correctly.
53 */
54 construct(score, badness, matchObj, toks, ...)
55 {
56 /* stash away the information */
57 gramBadness = badness;
58 gramMatchObj = matchObj;
59 gramTokens = toks;
60 }
61
62 /*
63 * The 'badness' value associated with the alternative. A value of
64 * zero means that there's no badness.
65 */
66 gramBadness = 0
67
68 /*
69 * the "match object" class - this is the class that
70 * GrammarProd.parseTokens() instantiates to represent a match to
71 * this alternative in the match list that the method returns
72 */
73 gramMatchObj = nil
74
75 /*
76 * The token descriptor list. This is a list of zero or more
77 * GrammarAltTokInfo objects describing the tokens making up this
78 * rule.
79 */
80 gramTokens = []
81 ;
82
83 /*
84 * Grammar rule token descriptor. GrammarProd.getGrammarInfo()
85 * instantiates one of these objects to represent each token slot in an
86 * alternative; a GrammarAltInfo object's gramTokens property has a list
87 * of these objects.
88 */
89 class GrammarAltTokInfo: object
90 /*
91 * Constructor. GrammarProd.getGrammarInfo() calls this once for
92 * each token in each alternative in the production, passing in
93 * values to fully describe the token slot: the target property (in a
94 * 'grammar' statement, this is the property after a '->' symbol);
95 * the token type; and extra information that depends on the token
96 * type. Note that we use '...' at the end of the argument list so
97 * that we'll be compatible with any future changes to GrammarProd
98 * that add more arguments to this method.
99 */
100 construct(prop, typ, info, ...)
101 {
102 /* remember the information */
103 gramTargetProp = prop;
104 gramTokenType = typ;
105 gramTokenInfo = info;
106 }
107
108 /*
109 * The target property - this is the property of the *match object*
110 * that will store the match information for the token. In a
111 * 'grammar' statement, this is the property after the '->' symbol
112 * for this token.
113 */
114 gramTargetProp = nil
115
116 /*
117 * The token type. This is one of the GramTokTypeXxx values (see
118 * gramprod.h) indicating what kind of token slot this is.
119 */
120 gramTokenType = nil
121
122 /*
123 * Detailed information for the token slot, which depends on the
124 * token type:
125 *
126 * GramTokTypeProd - this gives the GrammarProd object defining the
127 * sub-production that this token slot matches
128 *
129 * GramTokTypeSpeech - this is the property ID giving the
130 * part-of-speech property that this token slot matches
131 *
132 * GramTokTypeNSpeech - this is a list of property IDs giving the
133 * part-of-speech properties that this token slot matches
134 *
135 * GramTokTypeLiteral - this is a string giving the literal that this
136 * slot matches
137 *
138 * GramTokTypeTokEnum - this is the enum value giving the token type
139 * that this slot matches
140 *
141 * GramTokTypeStar - no extra information (the value will be nil)
142 */
143 gramTokenInfo = nil
144 ;
145
146 /*
147 * Dynamic match object interface. This is a mix-in class that should be
148 * used as a superclass for any class used as the match object when
149 * creating new alternatives dynamically with GrammarProd.addAlt().
150 *
151 * This class provides an implementation of grammarInfo() that works like
152 * the version the compiler generates for static match objects. In this
153 * case, we use the grammarAltProps information that addAlt() stores in
154 * the match object.
155 */
156 class DynamicProd: object
157 /*
158 * Generate match information. This returns the same information
159 * that grammarInfo() returns for match objects that the compiler
160 * generates for static 'grammar' statements.
161 */
162 grammarInfo()
163 {
164 return [grammarTag] + grammarAltProps.mapAll({ p: self.(p) });
165 }
166
167 /*
168 * grammarTag - the name for the collection of alternatives
169 * associated with the match object. This name is primarily for
170 * debugging purposes; it appears as the first element of the
171 * grammarInfo() result list.
172 */
173 grammarTag = 'new-alt'
174
175 /*
176 * grammarAltProps - the list of "->" properties used in all of the
177 * alternatives associated with this match object. addAlts() stores
178 * this list automatically - there's no need to create it manually.
179 */
180 grammarAltProps = []
181 ;