~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to third_party/ply/test/calclex.py

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -----------------------------------------------------------------------------
 
2
# calclex.py
 
3
# -----------------------------------------------------------------------------
 
4
import sys
 
5
 
 
6
if ".." not in sys.path: sys.path.insert(0,"..")
 
7
import ply.lex as lex
 
8
 
 
9
tokens = (
 
10
    'NAME','NUMBER',
 
11
    'PLUS','MINUS','TIMES','DIVIDE','EQUALS',
 
12
    'LPAREN','RPAREN',
 
13
    )
 
14
 
 
15
# Tokens
 
16
 
 
17
t_PLUS    = r'\+'
 
18
t_MINUS   = r'-'
 
19
t_TIMES   = r'\*'
 
20
t_DIVIDE  = r'/'
 
21
t_EQUALS  = r'='
 
22
t_LPAREN  = r'\('
 
23
t_RPAREN  = r'\)'
 
24
t_NAME    = r'[a-zA-Z_][a-zA-Z0-9_]*'
 
25
 
 
26
def t_NUMBER(t):
 
27
    r'\d+'
 
28
    try:
 
29
        t.value = int(t.value)
 
30
    except ValueError:
 
31
        print("Integer value too large %s" % t.value)
 
32
        t.value = 0
 
33
    return t
 
34
 
 
35
t_ignore = " \t"
 
36
 
 
37
def t_newline(t):
 
38
    r'\n+'
 
39
    t.lineno += t.value.count("\n")
 
40
    
 
41
def t_error(t):
 
42
    print("Illegal character '%s'" % t.value[0])
 
43
    t.lexer.skip(1)
 
44
    
 
45
# Build the lexer
 
46
lex.lex()
 
47
 
 
48
 
 
49