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