-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathply-impl.py
78 lines (58 loc) · 1.3 KB
/
ply-impl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# @author: Wai Yi Leung
import ply.lex as lex
# List of token names. This is always required
tokens = (
'FORM',
'FORMNAME',
# 'STATEMENTS'
'FIELDID',
'BRACKETOPEN',
'BRACKETCLOSE',
'FIELDLABEL',
'FIELD_QUESTION',
'BOOLEANTYPE'
)
literals = "+-*/"
# Regular expression rules for simple tokens
t_FORM = r'form '
t_FORMNAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
# t_STATEMENTS = r'\{ [.*]* \}'
t_FIELDID = r'[\w]+: '
# Define a rule so we can track line numbers
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_BRACKETOPEN(t):
r'{'
return t
def t_BRACKETCLOSE(t):
r'}'
return t
def t_FIELDLABEL(t):
r'[\w]+:'
return t
def t_FIELD_QUESTION(t):
r' \'[\w\d\?/ ]+\' '
return t
def t_BOOLEANTYPE(t):
r' boolean '
return t
# A string containing ignored characters (spaces and tabs)
t_ignore = ' \t'
# Error handling rule
def t_error(t):
print "Illegal character '%s'" % t.value[0]
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
##########
# data = open('tests/questionaire_skeleton.form').read()
data = open('tests/questionaire_basic.form').read()
# Give the lexer some input
lexer.input(data)
# Tokenize
while True:
tok = lexer.token()
if not tok: break # No more input
print tok