Skip to content

Commit 45aedd5

Browse files
author
Allen Short
committed
Merge branch 'custom_labels'
2 parents 3a622e9 + 9e7f047 commit 45aedd5

File tree

15 files changed

+514
-178
lines changed

15 files changed

+514
-178
lines changed

bin/move_stage

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
# -*- mode: python -*-
3+
import os
4+
import shutil
5+
6+
7+
def main():
8+
9+
bin_dir = os.path.dirname(os.path.realpath(__file__))
10+
parsley_dir = os.path.split(bin_dir)[0]
11+
stage_dir = os.path.join(parsley_dir, 'stage')
12+
if not os.path.exists(stage_dir):
13+
raise Exception('Stage dir does not exist')
14+
15+
move_pkg_gen(parsley_dir, stage_dir, 'ometa')
16+
move_pkg_gen(parsley_dir, stage_dir, 'terml')
17+
18+
19+
def move_pkg_gen(parsley_dir, stage_dir, pkg):
20+
print "\nMoving " + pkg
21+
stage_gen_dir = os.path.join(stage_dir, pkg, '_generated')
22+
gen_dir = os.path.join(parsley_dir, pkg, '_generated')
23+
24+
if os.path.exists(gen_dir):
25+
print "Removing " + os.path.relpath(gen_dir, parsley_dir)
26+
shutil.rmtree(gen_dir)
27+
28+
print 'Copying ' + os.path.relpath(stage_gen_dir, parsley_dir) + ' to ' + os.path.relpath(gen_dir, parsley_dir)
29+
shutil.copytree(stage_gen_dir, gen_dir)
30+
31+
32+
if __name__ == '__main__':
33+
main()
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+

bin/remove_stage

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# -*- mode: python -*-
3+
import os
4+
import shutil
5+
6+
7+
def main():
8+
9+
bin_dir = os.path.dirname(os.path.realpath(__file__))
10+
parsley_dir = os.path.split(bin_dir)[0]
11+
stage_dir = os.path.join(parsley_dir, 'stage')
12+
if os.path.exists(stage_dir):
13+
print 'Removing stage dir'
14+
shutil.rmtree(stage_dir)
15+
16+
if __name__ == '__main__':
17+
main()
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+

bin/stage

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
11
#!/usr/bin/env python
22
# -*- mode: python -*-
3-
import sys; sys.path.append(".")
4-
import os, glob, shutil, sys
3+
import sys
4+
import os
5+
import glob
6+
import shutil
57
from ometa.runtime import ParseError
68
from ometa.grammar import OMeta
79
from ometa.builder import writePython
810

911

12+
def main():
13+
bin_dir = os.path.dirname(os.path.realpath(__file__))
14+
parsley_dir = os.path.split(bin_dir)[0]
15+
stage_dir = os.path.join(parsley_dir, 'stage')
16+
sys.path.append(parsley_dir)
1017

11-
def stage_pkg(pkg):
12-
print "Copying", pkg
13-
shutil.copytree(pkg, 'stage/' + pkg,
18+
if os.path.exists(stage_dir):
19+
print "Removing stage dir"
20+
shutil.rmtree(stage_dir)
21+
22+
os.mkdir(stage_dir)
23+
stage_pkg(parsley_dir, stage_dir, 'ometa')
24+
stage_pkg(parsley_dir, stage_dir, 'terml')
25+
26+
27+
def stage_pkg(parsley_dir, stage_dir, pkg):
28+
print "\nCopying", pkg
29+
stage_pkg_dir = os.path.join(stage_dir, pkg)
30+
stage_gen_dir = os.path.join(stage_pkg_dir, '_generated')
31+
pkg_dir = os.path.join(parsley_dir, pkg)
32+
33+
shutil.copytree(pkg_dir, stage_pkg_dir,
1434
ignore=lambda src, names: [n for n in names if n.endswith('pyc')])
15-
for fn in glob.glob(pkg + "/*.parsley"):
16-
grammar = open(fn).read()
17-
grammarname = fn.split('/')[1].split('.')[0]
18-
pyfn = "stage/%s/_generated/%s.py" % (pkg, grammarname)
19-
print "%s => %s" % (fn, pyfn)
35+
for filename in glob.glob(pkg_dir + "/*.parsley"):
36+
grammar = open(filename).read()
37+
grammarname = os.path.split(filename)[1].split('.')[0]
38+
pyfn = os.path.join(stage_gen_dir, grammarname + '.py')
39+
print "{src:38} => {dst}".format(src=os.path.relpath(filename, parsley_dir),
40+
dst=os.path.relpath(pyfn, parsley_dir))
2041
g = OMeta(grammar)
2142
tree = g.parseGrammar(grammarname)
2243
source = writePython(tree)
2344
pythonFile = open(pyfn, 'w')
2445
pythonFile.write(source)
2546

2647

27-
os.mkdir('stage')
28-
stage_pkg('ometa')
29-
stage_pkg('terml')
48+
if __name__ == '__main__':
49+
main()
3050

3151

3252

doc/reference.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ Basic syntax
5959
``?(pythonExpression)``:
6060
Fail if the Python expression is false, Returns True otherwise.
6161

62+
``expr ^(CustomLabel)``:
63+
If the expr fails, the exception raised will contain CustomLabel.
64+
Good for providing more context when a rule is broken.
65+
CustomLabel can contain any character other than "(" and ")".
66+
6267
Comments like Python comments are supported as well, starting with #
6368
and extending to the end of the line.
6469

examples/exceptions.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
A grammar for parsing a tiny HTML-like language, plus a transformer for it.
3+
"""
4+
from parsley import makeGrammar, term, termMaker as t, unwrapGrammar
5+
from itertools import chain
6+
7+
tinyHTMLGrammar = r"""
8+
9+
name = <letterOrDigit+>
10+
11+
tag = ((('<' spaces name:n spaces attribute*:attrs '>')
12+
html:c
13+
('<' '/' token(n) spaces '>')
14+
-> t.Element(n.lower(), dict(attrs), c))) ^ (valid tag)
15+
16+
html = (text | tag)*
17+
18+
text = <(~('<') anything)+>
19+
20+
attribute = spaces name:k token('=') quotedString:v -> (k, v)
21+
22+
quotedString = ((('"' | '\''):q <(~exactly(q) anything)*>:xs exactly(q))
23+
-> xs)
24+
25+
"""
26+
TinyHTML = makeGrammar(tinyHTMLGrammar, globals(), name="TinyHTML")
27+
28+
testSource = "<html<title>Yes</title><body><h1>Man, HTML is <i>great</i>.</h1><p>How could you even <b>think</b> otherwise?</p><img src='HIPPO.JPG'></img><a href='http://twistedmatrix.com'>A Good Website</a></body></html>"
29+
30+
print unwrapGrammar(TinyHTML)(testSource).apply('tag')

examples/minml.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
22
A grammar for parsing a tiny HTML-like language, plus a transformer for it.
33
"""
4-
from parsley import makeGrammar, term, makeTerm as t
4+
from parsley import makeGrammar, term, termMaker as t
55
from itertools import chain
66

7-
tinyHTMLGrammar = """
7+
tinyHTMLGrammar = r"""
88
99
name = <letterOrDigit+>
1010
@@ -19,10 +19,12 @@
1919
2020
attribute = spaces name:k token('=') quotedString:v -> (k, v)
2121
22-
quotedString = (('"' | '\''):q <(~exactly(q) anything)*>:xs exactly(q)
22+
quotedString = (('"' | '\''):q <(~exactly(q) anything)*>:xs exactly(q))
2323
-> xs
2424
2525
"""
2626
TinyHTML = makeGrammar(tinyHTMLGrammar, globals(), name="TinyHTML")
2727

2828
testSource = "<html><title>Yes</title><body><h1>Man, HTML is <i>great</i>.</h1><p>How could you even <b>think</b> otherwise?</p><img src='HIPPO.JPG'></img><a href='http://twistedmatrix.com'>A Good Website</a></body></html>"
29+
30+
print TinyHTML(testSource).html()

0 commit comments

Comments
 (0)