root / cobra / trunk / Source / KeywordSpecs.cobra

Revision 1786, 12.0 kB (checked in by Chuck.Esterbrook, 5 weeks ago)

Added anonymous methods, also known as closures.

  • Property svn:eol-style set to native
Line 
1class KeywordSpecs
2        """
3        Specifies the keywords, their logical groups and documentation.
4       
5        The tokenizer obtains its list of keywords from this class.
6
7        Also, this information can be used for command line help and to generate documentation.
8        """
9
10        shared
11
12                var _keywords as List<of String>?
13
14                def keywords as List<of String>
15                        if _keywords is nil, _keywords = .unpackKeywords(.rawSpecs)
16                        return List<of String>(_keywords to !)
17
18                def unpackKeywords(specs as List<of String>) as List<of String>
19                        """
20                        Return a list of keyword names in no particular order.
21                        """
22                        keywords = List<of String>()
23                        for spec in specs
24                                i = spec.indexOf('|')
25                                assert i > 0
26                                name = spec[:i].trim
27                                if name.endsWith('(r)')
28                                        name = name[:-3].trim
29                                keywords.add(name)
30                        return keywords
31
32                var _groups as Dictionary<of String, List<of Keyword>>?
33
34                def groups as Dictionary<of String, List<of Keyword>>
35                        if _groups is nil, _groups = .unpackGroups(.rawSpecs)
36                        return _groups to !
37
38                def unpackGroups(specs as List<of String>) as Dictionary<of String, List<of Keyword>>
39                        """
40                        Return a dictionary of keyword groups.
41                        """
42                        groups = Dictionary<of String, List<of Keyword>>()
43                        for spec in specs
44                                parts = spec.trim.split(c'|')
45                                assert parts.length >= 2
46                                name = parts[0].trim
47                                if name.endsWith('(r)')
48                                        reservedForFutureUse = true
49                                        name = name[:-3].trim
50                                else
51                                        reservedForFutureUse = false
52                                for part in parts  # CC: parts[1:]
53                                        i = part.indexOf(':')
54                                        if i == -1, continue
55                                        group = part[:i].trim
56                                        description = part[i+1:].trim
57                                        groups[group].add(Keyword(name, description, reservedForFutureUse))
58                        return groups
59
60                get rawSpecs as List<of String>
61                        # format:
62                        #     word | explanation 1 | explanation 2 | ... | explanation N
63                        # a 'word' can be suffixed with ' (r)' to indicate 'reserved'
64                        # an 'explanation' is: group: text
65                        # where 'group' is the group of keywords that the explanation belongs to
66                        return [
67                                'use         | module: Specify namespaces used in module.',
68                                'import (r)  | module: From another Cobra module, import all the "use" clauses and declare the same namespace.',
69                                'namespace   | declarative: Specify the namespace that subsequent declarations reside in.',
70                                'enum        | declarative: Declare an enumeration type.',
71                                'class       | declarative: Declare a class type.',
72                                'inherits    | declarative: Specify inheritance for a class or interface. | operator: Test type inheritance.',
73                                'implements  | declarative: Specify the interfaces that a class implements. | operator: Test interface implementation.',
74                                'interface   | declarative: Declare an interface.',
75                                'struct      | declarative: Declare a struct.',
76                                'extend      | declarative: Declare extension methods on an existing type.',
77                                'where       | declarative: Specify constraints on generic parameters. | expression: The filtering condition on a `for` expression.',
78                                'must        | declarative: Specify constraints on generic parameters (`must be`).',
79                                'be          | declarative: Specify constraints on generic parameters (`must be`).',
80                                'callable    | declarative: Specify constraint that generic type argument must be callable/instantiable.',
81                                'cue         | declarative: Reserved for future use.',
82                                'invariant   | declarative, contracts: Declare invariant conditions that are true on objects after any public call.',
83                                'def         | declarative: Declare a method.',
84                                'sig         | declarative: Declare a method signature.',
85                                'as          | declarative: Declare the type for an argument, variable, method return, property, etc.',
86                                'get         | declarative: Declare property reader/accessor. | expression: The evaluation result on a `for` expression.',
87                                'set         | declarative: Declare property writer/mutator.',
88                                'pro         | declarative: Declare a property.',
89                                'var         | declarative: Declare a variable at the object or type level. Also, used as `from var` in property declarations. | expression: Refer to the variable that matches the containing property name.',
90                                'from        | declarative: Specify the backing variable for a property. Also, may be used for future LINQ syntax.',
91                                'has         | declarative: Specify attributes to attach to the containing declaration.',
92                                'body        | declarative: Declare the body of a method, indexer or property after blocked contracts or unit tests.',
93                                'test        | declarative: Declare a unit test attached to the containing declaration.',
94                                'shared      | declarative: Specify that a member is shared among all instances. (Called `static` in some languages.)',
95                                'is          | declarative: Specify modifies on a member such as `override` and `shared`.',
96                                'event       | declarative: Declare an event.',
97                                'of          | declarative: Specify the underlying type of an enumeration. Also, specify type arguments to a generic type.',
98                                'inlined (r) | declarative: ',
99                                'abstract    | modifier: Specify that the member is abstract, e.g., without implementation.',
100                                'extern      | modifier: Specify that a class is external to the Cobra code.',
101                                'fake        | modifier: Specify that a class is external to the Cobra code. Deprecated 2008-10-03.',
102                                'internal    | modifier: Specify that the member is visible within the entire assembly only.',
103                                'new         | modifier: Specify that the member is a new member, distinct from the one in the base class with the same name.',
104                                'nonvirtual  | modifier: Specify that the member cannot be overidden. NOT the default.',
105                                'override    | modifier: Specify that the member is overriding the same member in an ancestor type.',
106                                'partial     | modifier: Specify that the type implementation is split across declarations; usually across files.',
107                                'public      | modifier: Specify that a type or member is visible to all. The default, unless a name starts with an underscore.',
108                                'private     | modifier: Specify that the member is visible to this type only. The default for members starting with two underscores.',
109                                'protected   | modifier: Specify that the member is visible to this type and all descendant types. The default for members starting with one underscore.',
110                                'virtual     | modifier: Specify that the member can be overidden. On by default.',
111                                'bool        | type: The Boolean data type.',
112                                'char        | type: The (Unicode) character data type.',
113                                'int         | type: The default integer data type; signed 32-bit. Alias for `int32`.',
114                                'uint        | type: The unsigned integer data type; unsigned 32-bit. Alias for `uint32`.',
115                                'decimal     | type: The default decimal data type for accurate, but slower, decimal point arithmetic.',
116                                'float       | type: The floating point type, 64-bit, for less accurate, but faster decimal point arithmetic. Alias for `float64`.',
117                                'dynamic     | type: The `dynamic` type for which all operations become late bound.`',
118                                'same (r)    | type: The type indicating the same type as the current instance which will always be the same as the enclosing type or a descendant thereof.',
119                                'number      | type: The numeric type which defaults to `decimal`. Via command line args or directives, can be set to a different decimal point type.',
120                                'require     | contracts: Declare the preconditions of a method, property or indexer.',
121                                'ensure      | contracts: Declare the postconditions of a method, property or indexer.',
122                                'old         | contracts: Access the old, or original, value of an expression before the method executed.',
123                                'implies     | contracts: Express a conditional in a contract.',
124                                'assert      | statement: Assert that a condition is true, or throw an AssertException.',
125                                'branch      | statement: Branch control against multiple choices. (Called `switch` in some languages.)',
126                                'on          | statement: Used for `branch` and `trace` statements.',
127                                'off         | statement: Used for `trace` statements.',
128                                'expect      | statement: Expect and trap an exception, or throw an ExpectException.',
129                                'if          | statement: The classic `if` statement. Execute statements if a condition is true.',
130                                'else        | statement: The `else` portion of an `if-else` statement.',
131                                'using       | statement: Use a resource and dispose it afterwards.',
132                                'while       | statement: The classic `while` statement. Execute statements while a condition is true.',
133                                'post        | statement: The `post while` loop evaluates its expression after the statements execute.',
134                                'for         | statement: Enumerate through objects or numbers.',
135                                'break       | statement: Break out of the closest loop.',
136                                'continue    | statement: Continue at the top of the closest loop.',
137                                'try         | statement: Start an exception catch/trap boundary block.',
138                                'catch       | statement: Catch/trap an exception type or all exceptions.',
139                                'success     | statement: Act when catch/trap block succeeds with no exception.',
140                                'finally     | statement: Act regardless of exception catch/trap success.',
141                                'throw       | statement: Throw an exception.',
142                                'pass        | statement: Placeholder for an empty block.',
143                                'print       | statement: Print output.',
144                                'stop        | statement: Stop the newline after a print.',
145                                'trace       | statement: Emit trace output for debugging purposes.',
146                                'return      | statement: Return a value from a method, property or indexer.',
147                                'yield       | statement: Yield a value from a method, property or indexer.',
148                                'listen      | statement: Listen to an event.',
149                                'ignore      | statement: Stop listening to an event.',
150                                'raise       | statement: Raise an event.',
151                                'end (r)     | statement: For Python Server Pages (PSP) style "end syntax".',
152                                'do          | expression: Start an anonymous method (aka "closure").',
153                                'ref         | expression: Refer to a method without invoking it.',
154                                'this        | expression: Reference to the current object/instance at run-time.',
155                                'base        | expression: Reference to the base class.',
156                                'to          | expression: Cast an expression to a type or throw InvalidCastException.',
157                                'to\\?       | expression: Cast an expression to a type or evaluate to `nil`.',
158                                'and         | expression: Logical "and". | contracts: "and ensure" to strengthen postconditions in an overriding method.',
159                                'or          | expression: Logical "or". | contracts: "or require" to weaken preconditions in an overriding method.',
160                                'not         | expression: Logical negation.',
161                                'any         | expression: Evaluate to true if any element in a sequence is true.',
162                                'every (r)   | expression:',
163                                'all         | expression: Evaluate to true if all elements in a sequent is true.',
164                                'each (r)    | expression:',
165                                'in          | expression: Test inclusion | arg modifier: Value passed into method only (the default).',
166                                "passthrough | expression: Psuedo-type used in typecasting in order to bypass the compiler's typechecking. Code pass through to the backend (C#, Java, whatever).",
167                                'true        | expression: The Boolean value `true`.',
168                                'false       | expression: The Boolean value `value`.',
169                                'nil         | expression: The special value `nil`. Called `null`, `None` and `Nothing` in some languages.',
170                                'vari        | arg modifier: Specify that the parameter captures variable number of arguments.',
171                                'out         | arg modifier: Value returned/set by method, but not passed in.',
172                                'inout       | arg modifier: Value passed in and returned/set by the method.',
173                                'objc (r)    | misc: For potential Objective-C integration.',
174                                'except      | misc: Catch Python syntax for exceptions to give helpful error message.',
175                        ]
176
177
178class Keyword
179        """
180        Used by KeywordSpecs.unpackGroups to capture the information about a keyword including
181        its name, description and whether or not it's reserved for future use.
182        """
183
184        var _name as String
185        var _description as String
186        var _reservedForFutureUse as bool
187       
188        def init(name as String, description as String, reservedForFutureUse as bool)
189                _name = name
190                _description = description
191                _reservedForFutureUse = reservedForFutureUse
192
193        get name from var
194
195        get description from var
196
197        get reservedForFutureUse from var
Note: See TracBrowser for help on using the browser.