summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-09-24 17:02:48 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-09-24 17:02:48 +0000
commit6316cc5922201d5157a3eedc92c319c99e7a8579 (patch)
tree45b9df9b17033ebe5669bc69b7bacb7698794260
parent7495b8f797419780718394a8597d493e9e839f06 (diff)
downloadpyparsing-6316cc5922201d5157a3eedc92c319c99e7a8579.tar.gz
Updated some examples to more current pyparsing coding methods
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@442 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/examples/idlParse.py73
-rw-r--r--src/examples/lucene_grammar.py41
-rw-r--r--src/examples/oc.py107
-rw-r--r--src/examples/simpleSQL.py20
4 files changed, 86 insertions, 155 deletions
diff --git a/src/examples/idlParse.py b/src/examples/idlParse.py
index dd556e5..b94bac4 100644
--- a/src/examples/idlParse.py
+++ b/src/examples/idlParse.py
@@ -19,76 +19,27 @@ def CORBA_IDL_BNF():
if not bnf:
# punctuation
- colon = Literal(":")
- lbrace = Literal("{")
- rbrace = Literal("}")
- lbrack = Literal("[")
- rbrack = Literal("]")
- lparen = Literal("(")
- rparen = Literal(")")
- equals = Literal("=")
- comma = Literal(",")
- dot = Literal(".")
- slash = Literal("/")
- bslash = Literal("\\")
- star = Literal("*")
- semi = Literal(";")
- langle = Literal("<")
- rangle = Literal(">")
+ (colon,lbrace,rbrace,lbrack,rbrack,lparen,rparen,
+ equals,comma,dot,slash,bslash,star,semi,langle,rangle) = map(Literal, r":{}[]()=,./\*;<>")
# keywords
- any_ = Keyword("any")
- attribute_ = Keyword("attribute")
- boolean_ = Keyword("boolean")
- case_ = Keyword("case")
- char_ = Keyword("char")
- const_ = Keyword("const")
- context_ = Keyword("context")
- default_ = Keyword("default")
- double_ = Keyword("double")
- enum_ = Keyword("enum")
- exception_ = Keyword("exception")
- false_ = Keyword("FALSE")
- fixed_ = Keyword("fixed")
- float_ = Keyword("float")
- inout_ = Keyword("inout")
- interface_ = Keyword("interface")
- in_ = Keyword("in")
- long_ = Keyword("long")
- module_ = Keyword("module")
- object_ = Keyword("Object")
- octet_ = Keyword("octet")
- oneway_ = Keyword("oneway")
- out_ = Keyword("out")
- raises_ = Keyword("raises")
- readonly_ = Keyword("readonly")
- sequence_ = Keyword("sequence")
- short_ = Keyword("short")
- string_ = Keyword("string")
- struct_ = Keyword("struct")
- switch_ = Keyword("switch")
- true_ = Keyword("TRUE")
- typedef_ = Keyword("typedef")
- unsigned_ = Keyword("unsigned")
- union_ = Keyword("union")
- void_ = Keyword("void")
- wchar_ = Keyword("wchar")
- wstring_ = Keyword("wstring")
+ (any_, attribute_, boolean_, case_, char_, const_, context_, default_, double_, enum_, exception_,
+ FALSE_, fixed_, float_, inout_, interface_, in_, long_, module_, Object_, octet_, oneway_, out_, raises_,
+ readonly_, sequence_, short_, string_, struct_, switch_, TRUE_, typedef_, unsigned_, union_, void_,
+ wchar_, wstring_) = map(Keyword, """any attribute boolean case char const context
+ default double enum exception FALSE fixed float inout interface in long module
+ Object octet oneway out raises readonly sequence short string struct switch
+ TRUE typedef unsigned union void wchar wstring""".split())
identifier = Word( alphas, alphanums + "_" ).setName("identifier")
- #~ real = Combine( Word(nums+"+-", nums) + dot + Optional( Word(nums) )
- #~ + Optional( CaselessLiteral("E") + Word(nums+"+-",nums) ) )
real = Regex(r"[+-]?\d+\.\d*([Ee][+-]?\d+)?").setName("real")
- #~ integer = ( Combine( CaselessLiteral("0x") + Word( nums+"abcdefABCDEF" ) ) |
- #~ Word( nums+"+-", nums ) ).setName("int")
integer = Regex(r"0x[0-9a-fA-F]+|[+-]?\d+").setName("int")
udTypeName = delimitedList( identifier, "::", combine=True ).setName("udType")
- # have to use longest match for type, in case a user-defined type name starts with a keyword type, like "stringSeq" or "longArray"
- typeName = ( any_ ^ boolean_ ^ char_ ^ double_ ^ fixed_ ^
- float_ ^ long_ ^ octet_ ^ short_ ^ string_ ^
- wchar_ ^ wstring_ ^ udTypeName ).setName("type")
+ typeName = ( any_ | boolean_ | char_ | double_ | fixed_ |
+ float_ | long_ | octet_ | short_ | string_ |
+ wchar_ | wstring_ | udTypeName ).setName("type")
sequenceDef = Forward().setName("seq")
sequenceDef << Group( sequence_ + langle + ( sequenceDef | typeName ) + rangle )
typeDef = sequenceDef | ( typeName + Optional( lbrack + integer + rbrack ) )
diff --git a/src/examples/lucene_grammar.py b/src/examples/lucene_grammar.py
index 179f25e..6e404d8 100644
--- a/src/examples/lucene_grammar.py
+++ b/src/examples/lucene_grammar.py
@@ -8,16 +8,14 @@
#
from pyparsing import (Literal, CaselessKeyword, Forward, Regex, QuotedString, Suppress,
- Optional, Group, FollowedBy, infixNotation, opAssoc, ParseException, ParserElement)
+ Optional, Group, FollowedBy, infixNotation, opAssoc, ParseException, ParserElement,
+ pyparsing_common)
ParserElement.enablePackrat()
COLON,LBRACK,RBRACK,LBRACE,RBRACE,TILDE,CARAT = map(Literal,":[]{}~^")
LPAR,RPAR = map(Suppress,"()")
-and_ = CaselessKeyword("AND")
-or_ = CaselessKeyword("OR")
-not_ = CaselessKeyword("NOT")
-to_ = CaselessKeyword("TO")
-keyword = and_ | or_ | not_
+and_, or_, not_, to_ = map(CaselessKeyword, "AND OR NOT TO".split())
+keyword = and_ | or_ | not_ | to_
expression = Forward()
@@ -32,11 +30,11 @@ required_modifier = Literal("+")("required")
prohibit_modifier = Literal("-")("prohibit")
integer = Regex(r"\d+").setParseAction(lambda t:int(t[0]))
proximity_modifier = Group(TILDE + integer("proximity"))
-number = Regex(r'\d+(\.\d+)?').setParseAction(lambda t:float(t[0]))
+number = pyparsing_common.fnumber()
fuzzy_modifier = TILDE + Optional(number, default=0.5)("fuzzy")
term = Forward()
-field_name = valid_word.copy().setName("fieldname")
+field_name = valid_word().setName("fieldname")
incl_range_search = Group(LBRACK + term("lower") + to_ + term("upper") + RBRACK)
excl_range_search = Group(LBRACE + term("lower") + to_ + term("upper") + RBRACE)
range_search = incl_range_search("incl_range") | excl_range_search("excl_range")
@@ -52,9 +50,9 @@ term.setParseAction(lambda t:[t] if 'field' in t or 'boost' in t else None)
expression << infixNotation(term,
[
(required_modifier | prohibit_modifier, 1, opAssoc.RIGHT),
- ((not_ | '!').setParseAction(lambda:"NOT"), 1, opAssoc.RIGHT),
- ((and_ | '&&').setParseAction(lambda:"AND"), 2, opAssoc.LEFT),
- (Optional(or_ | '||').setParseAction(lambda:"OR"), 2, opAssoc.LEFT),
+ ((not_ | '!').setParseAction(lambda: "NOT"), 1, opAssoc.RIGHT),
+ ((and_ | '&&').setParseAction(lambda: "AND"), 2, opAssoc.LEFT),
+ (Optional(or_ | '||').setParseAction(lambda: "OR"), 2, opAssoc.LEFT),
])
# test strings taken from grammar description doc, and TestQueryParser.java
@@ -265,7 +263,7 @@ tests = r"""
(*:*)
+*:* -*:*
the wizard of ozzy
- """.splitlines()
+ """
failtests = r"""
field:term:with:colon some more terms
@@ -313,18 +311,9 @@ failtests = r"""
a\:b\+c\~
a:b\c~
[ a\ TO a* ]
- """.splitlines()
-
-allpass = True
-for t in [_f for _f in map(str.strip,tests) if _f]:
- print(t)
- try:
- #~ expression.parseString(t,parseAll=True)
- print(expression.parseString(t,parseAll=True))
- except ParseException as pe:
- print(t)
- print(pe)
- allpass = False
- print('')
+ """
+
+success1, _ = expression.runTests(tests)
+success2, _ = expression.runTests(failtests, failureTests=True)
-print(("FAIL", "OK")[allpass])
+print(("FAIL", "OK")[success1 and success2])
diff --git a/src/examples/oc.py b/src/examples/oc.py
index 77ea195..42b9d48 100644
--- a/src/examples/oc.py
+++ b/src/examples/oc.py
@@ -9,85 +9,78 @@ http://www.ioccc.org/1996/august.hint
The following is a description of the OC grammar:
- OC grammar
- ==========
- Terminals are in quotes, () is used for bracketing.
+ OC grammar
+ ==========
+ Terminals are in quotes, () is used for bracketing.
- program: decl*
+ program: decl*
- decl: vardecl
- fundecl
+ decl: vardecl
+ fundecl
- vardecl: type NAME ;
- type NAME "[" INT "]" ;
+ vardecl: type NAME ;
+ type NAME "[" INT "]" ;
- fundecl: type NAME "(" args ")" "{" body "}"
+ fundecl: type NAME "(" args ")" "{" body "}"
- args: /*empty*/
- ( arg "," )* arg
+ args: /*empty*/
+ ( arg "," )* arg
- arg: type NAME
+ arg: type NAME
- body: vardecl* stmt*
+ body: vardecl* stmt*
- stmt: ifstmt
- whilestmt
- dowhilestmt
- "return" expr ";"
- expr ";"
- "{" stmt* "}"
- ";"
+ stmt: ifstmt
+ whilestmt
+ dowhilestmt
+ "return" expr ";"
+ expr ";"
+ "{" stmt* "}"
+ ";"
- ifstmt: "if" "(" expr ")" stmt
- "if" "(" expr ")" stmt "else" stmt
+ ifstmt: "if" "(" expr ")" stmt
+ "if" "(" expr ")" stmt "else" stmt
- whilestmt: "while" "(" expr ")" stmt
+ whilestmt: "while" "(" expr ")" stmt
- dowhilestmt: "do" stmt "while" "(" expr ")" ";"
+ dowhilestmt: "do" stmt "while" "(" expr ")" ";"
- expr: expr binop expr
- unop expr
- expr "[" expr "]"
- "(" expr ")"
- expr "(" exprs ")"
- NAME
- INT
- CHAR
- STRING
+ expr: expr binop expr
+ unop expr
+ expr "[" expr "]"
+ "(" expr ")"
+ expr "(" exprs ")"
+ NAME
+ INT
+ CHAR
+ STRING
- exprs: /*empty*/
- (expr ",")* expr
+ exprs: /*empty*/
+ (expr ",")* expr
- binop: "+" | "-" | "*" | "/" | "%" |
- "=" |
- "<" | "==" | "!="
+ binop: "+" | "-" | "*" | "/" | "%" |
+ "=" |
+ "<" | "==" | "!="
- unop: "!" | "-" | "*"
+ unop: "!" | "-" | "*"
- type: "int" stars
- "char" stars
+ type: "int" stars
+ "char" stars
- stars: "*"*
+ stars: "*"*
"""
from pyparsing import *
LPAR,RPAR,LBRACK,RBRACK,LBRACE,RBRACE,SEMI,COMMA = map(Suppress, "()[]{};,")
-INT = Keyword("int")
-CHAR = Keyword("char")
-WHILE = Keyword("while")
-DO = Keyword("do")
-IF = Keyword("if")
-ELSE = Keyword("else")
-RETURN = Keyword("return")
+INT, CHAR, WHILE, DO, IF, ELSE, RETURN = map(Keyword,
+ "int char while do if else return".split())
NAME = Word(alphas+"_", alphanums+"_")
integer = Regex(r"[+-]?\d+")
char = Regex(r"'.'")
string_ = dblQuotedString
-INT = Keyword("int")
-CHAR = Keyword("char")
TYPE = Group((INT | CHAR) + ZeroOrMore("*"))
expr = Forward()
@@ -148,23 +141,23 @@ int
putstr(char *s)
{
while(*s)
- putchar(*s++);
+ putchar(*s++);
}
int
fac(int n)
{
if (n == 0)
- return 1;
+ return 1;
else
- return n*fac(n-1);
+ return n*fac(n-1);
}
int
putn(int n)
{
if (9 < n)
- putn(n / 10);
+ putn(n / 10);
putchar((n%10) + '0');
}
@@ -184,10 +177,10 @@ main()
int i;
i = 0;
while(i < 10)
- facpr(i++);
+ facpr(i++);
return 0;
}
"""
-ast = program.parseString(test,parseAll=True)
+ast = program.parseString(test, parseAll=True)
ast.pprint()
diff --git a/src/examples/simpleSQL.py b/src/examples/simpleSQL.py
index 09c028e..6cde6ce 100644
--- a/src/examples/simpleSQL.py
+++ b/src/examples/simpleSQL.py
@@ -7,24 +7,22 @@
#
from pyparsing import Literal, CaselessLiteral, Word, delimitedList, Optional, \
Combine, Group, alphas, nums, alphanums, ParseException, Forward, oneOf, quotedString, \
- ZeroOrMore, restOfLine, Keyword, upcaseTokens, pyparsing_common
+ ZeroOrMore, restOfLine, CaselessKeyword, pyparsing_common
# define SQL tokens
selectStmt = Forward()
-SELECT = Keyword("select", caseless=True)
-FROM = Keyword("from", caseless=True)
-WHERE = Keyword("where", caseless=True)
+SELECT, FROM, WHERE = map(CaselessKeyword, "select from where".split())
ident = Word( alphas, alphanums + "_$" ).setName("identifier")
-columnName = ( delimitedList( ident, ".", combine=True ) ).setName("column name").addParseAction(upcaseTokens)
-columnNameList = Group( delimitedList( columnName ) )
-tableName = ( delimitedList( ident, ".", combine=True ) ).setName("table name").addParseAction(upcaseTokens)
-tableNameList = Group( delimitedList( tableName ) )
+columnName = delimitedList(ident, ".", combine=True).setName("column name")
+columnName.addParseAction(pyparsing_common.upcaseTokens)
+columnNameList = Group( delimitedList(columnName))
+tableName = delimitedList(ident, ".", combine=True).setName("table name")
+tableName.addParseAction(pyparsing_common.upcaseTokens)
+tableNameList = Group(delimitedList(tableName))
whereExpression = Forward()
-and_ = Keyword("and", caseless=True)
-or_ = Keyword("or", caseless=True)
-in_ = Keyword("in", caseless=True)
+and_, or_, in_ = map(CaselessKeyword, "and or in".split())
binop = oneOf("= != < > >= <= eq ne lt le gt ge", caseless=True)
realNum = pyparsing_common.real()