-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More example updates, PEP-8 names, f-strings.
- Loading branch information
Showing
6 changed files
with
172 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# | ||
# lineno_example.py | ||
# | ||
# an example of using the location value returned by pyparsing to | ||
# extract the line and column number of the location of the matched text, | ||
# or to extract the entire line of text. | ||
# | ||
# Copyright (c) 2006, Paul McGuire | ||
# | ||
import pyparsing as pp | ||
|
||
data = """Now is the time | ||
for all good men | ||
to come to the aid | ||
of their country.""" | ||
|
||
|
||
# demonstrate use of lineno, line, and col in a parse action | ||
def report_long_words(st, locn, toks): | ||
word = toks[0] | ||
if len(word) > 3: | ||
print( | ||
f"Found {word!r} on line {pp.lineno(locn, st)} at column {pp.col(locn, st)}" | ||
) | ||
print("The full line of text was:") | ||
print(f"{pp.line(locn, st)!r}") | ||
print(f" {'^':>{pp.col(locn, st)}}") | ||
print() | ||
|
||
|
||
wd = pp.Word(pp.alphas).set_parse_action(report_long_words) | ||
wd[1, ...].parse_string(data) | ||
|
||
|
||
# demonstrate returning an object from a parse action, containing more information | ||
# than just the matching token text | ||
class Token: | ||
def __init__(self, st, locn, tok_string): | ||
self.token_string = tok_string | ||
self.locn = locn | ||
self.source_line = pp.line(locn, st) | ||
self.line_no = pp.lineno(locn, st) | ||
self.col = pp.col(locn, st) | ||
|
||
def __str__(self): | ||
return f"{self.token_string!r} (line: {self.line_no}, col: {self.col})" | ||
|
||
|
||
def create_token_object(st, locn, toks): | ||
return Token(st, locn, toks[0]) | ||
|
||
|
||
wd = pp.Word(pp.alphas).set_parse_action(create_token_object) | ||
|
||
for token_obj in wd[1, ...].parse_string(data): | ||
print(token_obj) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,64 @@ | ||
# macroExpander.py | ||
# | ||
# Example pyparsing program for performing macro expansion, similar to | ||
# the C pre-processor. This program is not as fully-featured, simply | ||
# processing macros of the form: | ||
# #def xxx yyyyy | ||
# and replacing xxx with yyyyy in the rest of the input string. Macros | ||
# can also be composed using other macros, such as | ||
# #def zzz xxx+1 | ||
# Since xxx was previously defined as yyyyy, then zzz will be replaced | ||
# with yyyyy+1. | ||
# | ||
# Copyright 2007 by Paul McGuire | ||
# | ||
from pyparsing import * | ||
|
||
# define the structure of a macro definition (the empty term is used | ||
# to advance to the next non-whitespace character) | ||
identifier = Word(alphas + "_", alphanums + "_") | ||
macroDef = "#def" + identifier("macro") + empty + restOfLine("value") | ||
|
||
# define a placeholder for defined macros - initially nothing | ||
macroExpr = Forward() | ||
macroExpr << NoMatch() | ||
|
||
# global dictionary for macro definitions | ||
macros = {} | ||
|
||
# parse action for macro definitions | ||
def processMacroDefn(s, l, t): | ||
macroVal = macroExpander.transformString(t.value) | ||
macros[t.macro] = macroVal | ||
macroExpr << MatchFirst(map(Keyword, macros.keys())) | ||
return "#def " + t.macro + " " + macroVal | ||
|
||
|
||
# parse action to replace macro references with their respective definition | ||
def processMacroRef(s, l, t): | ||
return macros[t[0]] | ||
|
||
|
||
# attach parse actions to expressions | ||
macroExpr.setParseAction(processMacroRef) | ||
macroDef.setParseAction(processMacroDefn) | ||
|
||
# define pattern for scanning through the input string | ||
macroExpander = macroExpr | macroDef | ||
|
||
|
||
# test macro substitution using transformString | ||
testString = """ | ||
#def A 100 | ||
#def ALEN A+1 | ||
char Astring[ALEN]; | ||
char AA[A]; | ||
typedef char[ALEN] Acharbuf; | ||
""" | ||
|
||
print(macroExpander.transformString(testString)) | ||
print(macros) | ||
# macro_expander.py | ||
# | ||
# Example pyparsing program for performing macro expansion, similar to | ||
# the C pre-processor. This program is not as fully-featured, simply | ||
# processing macros of the form: | ||
# #def xxx yyyyy | ||
# and replacing xxx with yyyyy in the rest of the input string. Macros | ||
# can also be composed using other macros, such as | ||
# #def zzz xxx+1 | ||
# Since xxx was previously defined as yyyyy, then zzz will be replaced | ||
# with yyyyy+1. | ||
# | ||
# Copyright 2007, 2023 by Paul McGuire | ||
# | ||
import pyparsing as pp | ||
|
||
# define the structure of a macro definition (the empty term is used | ||
# to advance to the next non-whitespace character) | ||
identifier = pp.common.identifier | ||
macro_def = "#def" + identifier("macro") + pp.empty + pp.restOfLine("value") | ||
|
||
# define a placeholder for defined macros - initially nothing | ||
macro_expr = pp.Forward() | ||
macro_expr << pp.NoMatch() | ||
|
||
# global dictionary for macro definitions | ||
macros = {} | ||
|
||
|
||
# parse action for macro definitions | ||
def process_macro_defn(t): | ||
macro_val = macro_expander.transform_string(t.value) | ||
macros[t.macro] = macro_val | ||
macro_expr << pp.MatchFirst(map(pp.Keyword, macros)) | ||
return f"#def {t.macro} {macro_val}" | ||
|
||
|
||
# parse action to replace macro references with their respective definition | ||
def process_macro_ref(t): | ||
return macros[t[0]] | ||
|
||
|
||
# attach parse actions to expressions | ||
macro_expr.set_parse_action(process_macro_ref) | ||
macro_def.set_parse_action(process_macro_defn) | ||
|
||
# define pattern for scanning through the input string | ||
macro_expander = macro_expr | macro_def | ||
|
||
|
||
# test macro substitution using transformString | ||
test_string = """ | ||
#def A 100 | ||
#def ALEN A+1 | ||
char Astring[ALEN]; | ||
char AA[A]; | ||
typedef char[ALEN] Acharbuf; | ||
""" | ||
|
||
print(test_string) | ||
print("-" * 40) | ||
print(macro_expander.transform_string(test_string)) | ||
print(macros) |
Oops, something went wrong.