| 1 |
@ |
| 2 |
Noweb literate programming file for the DDLm _type.contents type |
| 3 |
specification using Yapps3. |
| 4 |
|
| 5 |
<<TypeContents_syntax>>= |
| 6 |
# To maximize python3/python2 compatibility |
| 7 |
from __future__ import print_function |
| 8 |
from __future__ import unicode_literals |
| 9 |
from __future__ import division |
| 10 |
from __future__ import absolute_import |
| 11 |
|
| 12 |
<<Helper functions>> |
| 13 |
%% |
| 14 |
parser TypeParser: |
| 15 |
<<Regular expressions>> |
| 16 |
<<Grammar specification>> |
| 17 |
%% |
| 18 |
|
| 19 |
@ Helper functions. |
| 20 |
|
| 21 |
We have a monitor function which we can call to save the last parsed |
| 22 |
value (and print, if we are debugging). We also have functions for |
| 23 |
stripping off delimiters from strings. Finally, we match up our |
| 24 |
loops after reading them in. Note that we have function stripextras, |
| 25 |
which is only for semicolon strings, and stripstring, which is for |
| 26 |
getting rid of the inverted commas. |
| 27 |
|
| 28 |
<<Helper functions>>= |
| 29 |
# |
| 30 |
# helper code: we define our match tokens |
| 31 |
lastval = '' |
| 32 |
def monitor(location,value): |
| 33 |
global lastval |
| 34 |
#print 'At %s: %s' % (location,repr(value)) |
| 35 |
lastval = repr(value) |
| 36 |
return value |
| 37 |
|
| 38 |
@ |
| 39 |
|
| 40 |
<<Regular expressions>>= |
| 41 |
# first handle whitespace |
| 42 |
ignore: "([ \t\n\r])" |
| 43 |
# now the tokens |
| 44 |
token container: "[A-Za-z]+\(" |
| 45 |
token identifier: "[A-Za-z]+" |
| 46 |
token c_c_b: "\)" |
| 47 |
token o_c_b: "\(" |
| 48 |
token comma: "\," |
| 49 |
token END: '$' |
| 50 |
|
| 51 |
@ |
| 52 |
|
| 53 |
The final returned value is a possible-nested list with string-valued |
| 54 |
entries, which can then be interpreted as simple types. |
| 55 |
|
| 56 |
<<Grammar specification>>= |
| 57 |
# now the rules |
| 58 |
|
| 59 |
rule input: ( (( |
| 60 |
base_element {{p = [base_element]}} |
| 61 |
( |
| 62 |
comma base_element {{p.append(base_element)}} # |
| 63 |
)* |
| 64 |
END {{if len(p)==1: p = p[0]}} |
| 65 |
) |
| 66 |
)) {{return p}} |
| 67 |
|
| 68 |
|
| 69 |
rule base_element: (container element_list c_c_b {{return element_list}} |
| 70 |
| |
| 71 |
identifier ) {{return identifier}} |
| 72 |
|
| 73 |
rule element_list: ( base_element {{p = [base_element]}} |
| 74 |
( comma base_element {{p.append(base_element)}} |
| 75 |
) * |
| 76 |
) {{return p}} |
| 77 |
|