summaryrefslogtreecommitdiff
path: root/trunk/src/examples/apicheck.py
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-09 00:23:49 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-09 00:23:49 +0000
commitb2c3ade75384efe76b8774b607e17fe98fab92ef (patch)
treeb162262f3f0a4bc976d45bed08ccfc6cc9a2eb23 /trunk/src/examples/apicheck.py
parent0be19d2d8545f1ac4b93ffd0d10524613837ba39 (diff)
downloadpyparsing-b2c3ade75384efe76b8774b607e17fe98fab92ef.tar.gz
TagTag for 2.1.6 release
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/tags/pyparsing_2.1.6@402 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
Diffstat (limited to 'trunk/src/examples/apicheck.py')
-rw-r--r--trunk/src/examples/apicheck.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/trunk/src/examples/apicheck.py b/trunk/src/examples/apicheck.py
new file mode 100644
index 0000000..4315ac9
--- /dev/null
+++ b/trunk/src/examples/apicheck.py
@@ -0,0 +1,55 @@
+# apicheck.py
+# A simple source code scanner for finding patterns of the form
+# [ procname1 $arg1 $arg2 ]
+# and verifying the number of arguments
+
+from pyparsing import *
+
+# define punctuation and simple tokens for locating API calls
+LBRACK,RBRACK,LBRACE,RBRACE = map(Suppress,"[]{}")
+ident = Word(alphas,alphanums+"_") | QuotedString("{",endQuoteChar="}")
+arg = "$" + ident
+
+# define an API call with a specific number of arguments - using '-'
+# will ensure that after matching procname, an incorrect number of args will
+# raise a ParseSyntaxException, which will interrupt the scanString
+def apiProc(name, numargs):
+ return LBRACK + Keyword(name)("procname") - arg*numargs + RBRACK
+
+# create an apiReference, listing all API functions to be scanned for, and
+# their respective number of arguments. Beginning the overall expression
+# with FollowedBy allows us to quickly rule out non-api calls while scanning,
+# since all of the api calls begin with a "["
+apiRef = FollowedBy("[") + MatchFirst([
+ apiProc("procname1", 2),
+ apiProc("procname2", 1),
+ apiProc("procname3", 2),
+ ])
+
+test = """[ procname1 $par1 $par2 ]
+ other code here
+ [ procname1 $par1 $par2 $par3 ]
+ more code here
+ [ procname1 $par1 ]
+ [ procname3 ${arg with spaces} $par2 ]"""
+
+
+# now explicitly iterate through the scanner using next(), so that
+# we can trap ParseSyntaxException's that would be raised due to
+# an incorrect number of arguments. If an exception does occur,
+# then see how we reset the input text and scanner to advance to the
+# next line of source code
+api_scanner = apiRef.scanString(test)
+while 1:
+ try:
+ t,s,e = next(api_scanner)
+ print("found %s on line %d" % (t.procname, lineno(s,test)))
+ except ParseSyntaxException as pe:
+ print("invalid arg count on line", pe.lineno)
+ print(pe.lineno,':',pe.line)
+ # reset api scanner to start after this exception location
+ test = "\n"*(pe.lineno-1)+test[pe.loc+1:]
+ api_scanner = apiRef.scanString(test)
+ except StopIteration:
+ break
+