summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-09 21:24:40 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-09 21:24:40 +0000
commitce68aafa5c2bf143f2f18c852960be162725ec71 (patch)
tree4f435c112b7cf434e7ec114aa196a5b45907e014
parentdfa93523eb2287944bb51f6545efccf1bb5eeb24 (diff)
downloadpyparsing-ce68aafa5c2bf143f2f18c852960be162725ec71.tar.gz
Add missing authorship attributions and copyrights to examples
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@404 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/examples/antlr_grammar.py1
-rw-r--r--src/examples/antlr_grammar_tests.py2
-rw-r--r--src/examples/apicheck.py3
-rw-r--r--src/examples/btpyparse.py3
-rw-r--r--src/examples/commasep.py3
-rw-r--r--src/examples/datetimeParseActions.py1
-rw-r--r--src/examples/dfmparse.py9
-rw-r--r--src/examples/ebnf.py4
-rw-r--r--src/examples/ebnftest.py10
-rw-r--r--src/examples/excelExpr.py10
-rw-r--r--src/examples/gen_ctypes.py7
-rw-r--r--src/examples/greetingInGreek.py2
-rw-r--r--src/examples/greetingInKorean.py2
-rw-r--r--src/examples/groupUsingListAllMatches.py5
-rw-r--r--src/examples/holaMundo.py16
-rw-r--r--src/examples/httpServerLogParser.py2
-rw-r--r--src/examples/jsonParser.py38
-rw-r--r--src/examples/list1.py20
18 files changed, 92 insertions, 46 deletions
diff --git a/src/examples/antlr_grammar.py b/src/examples/antlr_grammar.py
index adf877e..76c681c 100644
--- a/src/examples/antlr_grammar.py
+++ b/src/examples/antlr_grammar.py
@@ -5,6 +5,7 @@ Created on 4 sept. 2010
@author: luca
+Submitted by Luca DallOlio, September, 2010
(Minor updates by Paul McGuire, June, 2012)
'''
from pyparsing import Word, ZeroOrMore, printables, Suppress, OneOrMore, Group, \
diff --git a/src/examples/antlr_grammar_tests.py b/src/examples/antlr_grammar_tests.py
index 31aab29..c2c3d8d 100644
--- a/src/examples/antlr_grammar_tests.py
+++ b/src/examples/antlr_grammar_tests.py
@@ -2,6 +2,8 @@
Created on 4 sept. 2010
@author: luca
+
+Submitted by Luca DallOlio, September, 2010
'''
import unittest
import antlr_grammar
diff --git a/src/examples/apicheck.py b/src/examples/apicheck.py
index 4315ac9..7bca41a 100644
--- a/src/examples/apicheck.py
+++ b/src/examples/apicheck.py
@@ -2,6 +2,9 @@
# A simple source code scanner for finding patterns of the form
# [ procname1 $arg1 $arg2 ]
# and verifying the number of arguments
+#
+# Copyright (c) 2004-2016, Paul McGuire
+#
from pyparsing import *
diff --git a/src/examples/btpyparse.py b/src/examples/btpyparse.py
index f3c11ae..6720c19 100644
--- a/src/examples/btpyparse.py
+++ b/src/examples/btpyparse.py
@@ -5,7 +5,8 @@ A standalone parser using pyparsing.
pyparsing has a simple and expressive syntax so the grammar is easy to read and
write.
-Matthew Brett 2010
+Submitted by Matthew Brett, 2010
+
Simplified BSD license
"""
diff --git a/src/examples/commasep.py b/src/examples/commasep.py
index 7696871..3ce8546 100644
--- a/src/examples/commasep.py
+++ b/src/examples/commasep.py
@@ -5,6 +5,9 @@
# - leading and trailing whitespace is implicitly trimmed from list elements
# - list elements can be quoted strings, which can safely contain commas without breaking
# into separate elements
+#
+# Copyright (c) 2004-2016, Paul McGuire
+#
from pyparsing import commaSeparatedList
diff --git a/src/examples/datetimeParseActions.py b/src/examples/datetimeParseActions.py
index 26d96a3..4d726c5 100644
--- a/src/examples/datetimeParseActions.py
+++ b/src/examples/datetimeParseActions.py
@@ -21,6 +21,7 @@ integer.setParseAction(convertToInt)
# define a pattern for a year/month/day date
date_expr = integer('year') + '/' + integer('month') + '/' + integer('day')
+date_expr.ignore(pythonStyleComment)
def convertToDatetime(s,loc,tokens):
try:
diff --git a/src/examples/dfmparse.py b/src/examples/dfmparse.py
index 96afbad..cf83814 100644
--- a/src/examples/dfmparse.py
+++ b/src/examples/dfmparse.py
@@ -127,13 +127,15 @@ def printer(s, loc, tok):
def get_filename_list(tf):
import sys, glob
if tf == None:
- tf = sys.argv[1:]
+ if len(sys.argv) > 1:
+ tf = sys.argv[1:]
+ else:
+ tf = glob.glob("*.dfm")
elif type(tf) == str:
tf = [tf]
testfiles = []
for arg in tf:
- for i in glob.glob(arg):
- testfiles.append(i)
+ testfiles.extend(glob.glob(arg))
return testfiles
def main(testfiles=None, action=printer):
@@ -145,6 +147,7 @@ def main(testfiles=None, action=printer):
Otherwise, a simple ParseResults is returned.
"""
testfiles = get_filename_list(testfiles)
+ print(testfiles)
if action:
for i in (simple_identifier, value, item_list):
diff --git a/src/examples/ebnf.py b/src/examples/ebnf.py
index 8dfcaea..242aed4 100644
--- a/src/examples/ebnf.py
+++ b/src/examples/ebnf.py
@@ -4,7 +4,9 @@
# ISO 14977 standardize The Extended Backus-Naur Form(EBNF) syntax.
# You can read a final draft version here:
# http://www.cl.cam.ac.uk/~mgk25/iso-ebnf.html
-
+#
+# Submitted 2004 by Seo Sanghyeon
+#
from pyparsing import *
diff --git a/src/examples/ebnftest.py b/src/examples/ebnftest.py
index 32c7fed..253404f 100644
--- a/src/examples/ebnftest.py
+++ b/src/examples/ebnftest.py
@@ -1,9 +1,15 @@
+#
+# ebnftest.py
+#
+# Test script for ebnf.py
+#
+# Submitted 2004 by Seo Sanghyeon
+#
print('Importing pyparsing...')
from pyparsing import *
print('Constructing EBNF parser with pyparsing...')
import ebnf
-import sets
grammar = '''
@@ -39,7 +45,7 @@ parsers = ebnf.parse(grammar, table)
ebnf_parser = parsers['syntax']
commentcharcount = 0
-commentlocs = sets.Set()
+commentlocs = set()
def tallyCommentChars(s,l,t):
global commentcharcount,commentlocs
# only count this comment if we haven't seen it before
diff --git a/src/examples/excelExpr.py b/src/examples/excelExpr.py
index 2700100..b98115e 100644
--- a/src/examples/excelExpr.py
+++ b/src/examples/excelExpr.py
@@ -59,10 +59,10 @@ expr << (arithExpr | textExpr)
(EQ + expr).runTests("""\
- =3*A7+5"
- =3*Sheet1!$A$7+5"
- =3*'Sheet 1'!$A$7+5"
- =3*'O''Reilly''s sheet'!$A$7+5"
- =if(Sum(A1:A25)>42,Min(B1:B25),if(Sum(C1:C25)>3.14, (Min(C1:C25)+3)*18,Max(B1:B25)))"
+ =3*A7+5
+ =3*Sheet1!$A$7+5
+ =3*'Sheet 1'!$A$7+5"
+ =3*'O''Reilly''s sheet'!$A$7+5
+ =if(Sum(A1:A25)>42,Min(B1:B25),if(Sum(C1:C25)>3.14, (Min(C1:C25)+3)*18,Max(B1:B25)))
=sum(a1:a25,10,min(b1,c2,d3))
""") \ No newline at end of file
diff --git a/src/examples/gen_ctypes.py b/src/examples/gen_ctypes.py
index 2c909e4..9e74819 100644
--- a/src/examples/gen_ctypes.py
+++ b/src/examples/gen_ctypes.py
@@ -1,3 +1,10 @@
+#
+# gen_ctypes.py
+#
+# Parse a .h header file to generate ctypes argtype and return type definitions
+#
+# Copyright 2004-2016, by Paul McGuire
+#
from pyparsing import *
typemap = {
diff --git a/src/examples/greetingInGreek.py b/src/examples/greetingInGreek.py
index 07a8f0a..c4d002f 100644
--- a/src/examples/greetingInGreek.py
+++ b/src/examples/greetingInGreek.py
@@ -4,6 +4,8 @@
#
# Demonstration of the parsing module, on the prototypical "Hello, World!" example
#
+# Copyright 2004-2016, by Paul McGuire
+#
from pyparsing import Word
# define grammar
diff --git a/src/examples/greetingInKorean.py b/src/examples/greetingInKorean.py
index d0ea0e5..e48cc8b 100644
--- a/src/examples/greetingInKorean.py
+++ b/src/examples/greetingInKorean.py
@@ -4,6 +4,8 @@
#
# Demonstration of the parsing module, on the prototypical "Hello, World!" example
#
+# Copyright 2004-2016, by Paul McGuire
+#
from pyparsing import Word, srange
koreanChars = srange(r"[\0xac00-\0xd7a3]")
diff --git a/src/examples/groupUsingListAllMatches.py b/src/examples/groupUsingListAllMatches.py
index d5037b8..28cdfd6 100644
--- a/src/examples/groupUsingListAllMatches.py
+++ b/src/examples/groupUsingListAllMatches.py
@@ -5,6 +5,8 @@
# This example performs work similar to itertools.groupby, but without
# having to sort the input first.
#
+# Copyright 2004-2016, by Paul McGuire
+#
from pyparsing import Word, ZeroOrMore, nums
aExpr = Word("A", nums)
@@ -12,5 +14,4 @@ bExpr = Word("B", nums)
cExpr = Word("C", nums)
grammar = ZeroOrMore(aExpr("A*") | bExpr("B*") | cExpr("C*"))
-results = grammar.parseString("A1 B1 A2 C1 B2 A3")
-print(results.dump())
+grammar.runTests("A1 B1 A2 C1 B2 A3")
diff --git a/src/examples/holaMundo.py b/src/examples/holaMundo.py
index 357e3c8..6ae2cc5 100644
--- a/src/examples/holaMundo.py
+++ b/src/examples/holaMundo.py
@@ -18,13 +18,21 @@ tokens = saludo.parseString("Hola, Mundo !")
for i in range(len(tokens)):
print ("Token %d -> %s" % (i,tokens[i]))
-#imprimimos cada uno de los tokens Y listooo!!, he aquí la salida
-# Token 0—> Hola Token 1—> , Token 2—> Mundo Token 3—> !
+#imprimimos cada uno de los tokens Y listooo!!, he aquí a salida
+# Token 0 -> Hola
+# Token 1 -> ,
+# Token 2-> Mundo
+# Token 3 -> !
-# Por supuesto, se pueden “reutilizar” gramáticas, por ejemplo:
+# Por supuesto, se pueden "reutilizar" gramáticas, por ejemplo:
numimag = Word(nums) + 'i'
numreal = Word(nums)
numcomplex = numreal + '+' + numimag
print (numcomplex.parseString("3+5i"))
-# Excelente!!, bueno, los dejo, me voy a seguir tirando código…
+# Cambiar a complejo numero durante parsear:
+numcomplex.setParseAction(lambda t: complex(''.join(t).replace('i','j')))
+print (numcomplex.parseString("3+5i"))
+
+# Excelente!!, bueno, los dejo, me voy a seguir tirando código...
+
diff --git a/src/examples/httpServerLogParser.py b/src/examples/httpServerLogParser.py
index 1a808ae..a147a05 100644
--- a/src/examples/httpServerLogParser.py
+++ b/src/examples/httpServerLogParser.py
@@ -1,5 +1,7 @@
# httpServerLogParser.py
#
+# Copyright (c) 2016, Paul McGuire
+#
"""
Parser for HTTP server log output, of the form:
diff --git a/src/examples/jsonParser.py b/src/examples/jsonParser.py
index 45cdef3..f080c6c 100644
--- a/src/examples/jsonParser.py
+++ b/src/examples/jsonParser.py
@@ -8,6 +8,8 @@
# Updated 8 Jan 2007 - fixed dict grouping bug, and made elements and
# members optional in array and object collections
#
+# Updated 9 Aug 2016 - use more current pyparsing constructs/idioms
+#
json_bnf = """
object
{ members }
@@ -33,35 +35,29 @@ value
from pyparsing import *
-TRUE = Keyword("true").setParseAction( replaceWith(True) )
-FALSE = Keyword("false").setParseAction( replaceWith(False) )
-NULL = Keyword("null").setParseAction( replaceWith(None) )
+def make_keyword(kwd_str, kwd_value):
+ return Keyword(kwd_str).setParseAction(replaceWith(kwd_value))
+TRUE = make_keyword("true", True)
+FALSE = make_keyword("false", False)
+NULL = make_keyword("null", None)
+
+LBRACK, RBRACK, LBRACE, RBRACE, COLON = map(Suppress, "[]{}:")
-jsonString = dblQuotedString.setParseAction( removeQuotes )
-jsonNumber = Combine( Optional('-') + ( '0' | Word('123456789',nums) ) +
- Optional( '.' + Word(nums) ) +
- Optional( Word('eE',exact=1) + Word(nums+'+-',nums) ) )
+jsonString = dblQuotedString().setParseAction(removeQuotes)
+jsonNumber = pyparsing_common.number()
jsonObject = Forward()
jsonValue = Forward()
jsonElements = delimitedList( jsonValue )
-jsonArray = Group(Suppress('[') + Optional(jsonElements, []) + Suppress(']') )
-jsonValue << ( jsonString | jsonNumber | Group(jsonObject) | jsonArray | TRUE | FALSE | NULL )
-memberDef = Group( jsonString + Suppress(':') + jsonValue )
-jsonMembers = delimitedList( memberDef )
-jsonObject << Dict( Suppress('{') + Optional(jsonMembers) + Suppress('}') )
+jsonArray = Group(LBRACK + Optional(jsonElements, []) + RBRACK)
+jsonValue << (jsonString | jsonNumber | Group(jsonObject) | jsonArray | TRUE | FALSE | NULL)
+memberDef = Group(jsonString + COLON + jsonValue)
+jsonMembers = delimitedList(memberDef)
+jsonObject << Dict(LBRACE + Optional(jsonMembers) + RBRACE)
jsonComment = cppStyleComment
-jsonObject.ignore( jsonComment )
+jsonObject.ignore(jsonComment)
-def convertNumbers(s,l,toks):
- n = toks[0]
- try:
- return int(n)
- except ValueError as ve:
- return float(n)
-
-jsonNumber.setParseAction( convertNumbers )
if __name__ == "__main__":
testdata = """
diff --git a/src/examples/list1.py b/src/examples/list1.py
index e410070..49a0bff 100644
--- a/src/examples/list1.py
+++ b/src/examples/list1.py
@@ -1,3 +1,10 @@
+#
+# list1.py
+#
+# an example of using parse actions to convert type of parsed data.
+#
+# Copyright (c) 2006-2016, Paul McGuire
+#
from pyparsing import *
# first pass
@@ -20,8 +27,8 @@ print(listStr.parseString(test))
lbrack = Literal("[").suppress()
rbrack = Literal("]").suppress()
cvtInt = lambda s,l,toks: int(toks[0])
-integer = Word(nums).setName("integer").setParseAction( cvtInt )
cvtReal = lambda s,l,toks: float(toks[0])
+integer = Word(nums).setName("integer").setParseAction( cvtInt )
real = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +
Optional(Word(nums))).setName("real").setParseAction( cvtReal )
listItem = real | integer | quotedString.setParseAction( removeQuotes )
@@ -33,14 +40,13 @@ test = "['a', 100, 3.14]"
print(listStr.parseString(test))
# third pass, add nested list support
-cvtInt = lambda s,l,toks: int(toks[0])
-cvtReal = lambda s,l,toks: float(toks[0])
+lbrack, rbrack = map(Suppress, "[]")
+
+cvtInt = tokenMap(int)
+cvtReal = tokenMap(float)
-lbrack = Literal("[").suppress()
-rbrack = Literal("]").suppress()
integer = Word(nums).setName("integer").setParseAction( cvtInt )
-real = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +
- Optional(Word(nums))).setName("real").setParseAction( cvtReal )
+real = Regex(r"[+-]?\d+\.\d*").setName("real").setParseAction( cvtReal )
listStr = Forward()
listItem = real | integer | quotedString.setParseAction(removeQuotes) | Group(listStr)