summaryrefslogtreecommitdiff
path: root/examples/idlParse.py
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2019-10-31 21:10:28 -0700
committerPaul McGuire <ptmcg@users.noreply.github.com>2019-10-31 23:10:28 -0500
commit53d1b4a6f48a53c4c4ec4ac7031362b691c0366d (patch)
tree088ad3cf3561b78a00af4fb2fd474f4a2b8ca70c /examples/idlParse.py
parent41752aa52cc97c710474bb2972cceab057b52ad4 (diff)
downloadpyparsing-git-53d1b4a6f48a53c4c4ec4ac7031362b691c0366d.tar.gz
Blacken the project (#141)
Diffstat (limited to 'examples/idlParse.py')
-rw-r--r--examples/idlParse.py205
1 files changed, 160 insertions, 45 deletions
diff --git a/examples/idlParse.py b/examples/idlParse.py
index 1daaf24..52ed3c3 100644
--- a/examples/idlParse.py
+++ b/examples/idlParse.py
@@ -6,85 +6,200 @@
# Copyright (c) 2003, Paul McGuire
#
-from pyparsing import Literal, Word, OneOrMore, ZeroOrMore, \
- Forward, delimitedList, Group, Optional, alphas, restOfLine, cStyleComment, \
- alphanums, quotedString, ParseException, Keyword, Regex
+from pyparsing import (
+ Literal,
+ Word,
+ OneOrMore,
+ ZeroOrMore,
+ Forward,
+ delimitedList,
+ Group,
+ Optional,
+ alphas,
+ restOfLine,
+ cStyleComment,
+ alphanums,
+ quotedString,
+ ParseException,
+ Keyword,
+ Regex,
+)
import pprint
-#~ import tree2image
+
+# ~ import tree2image
bnf = None
+
+
def CORBA_IDL_BNF():
global bnf
if not bnf:
# punctuation
- (colon,lbrace,rbrace,lbrack,rbrack,lparen,rparen,
- equals,comma,dot,slash,bslash,star,semi,langle,rangle) = map(Literal, r":{}[]()=,./\*;<>")
+ (
+ colon,
+ lbrace,
+ rbrace,
+ lbrack,
+ rbrack,
+ lparen,
+ rparen,
+ equals,
+ comma,
+ dot,
+ slash,
+ bslash,
+ star,
+ semi,
+ langle,
+ rangle,
+ ) = map(Literal, r":{}[]()=,./\*;<>")
# keywords
- (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
+ (
+ 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())
+ TRUE typedef unsigned union void wchar wstring""".split(),
+ )
- identifier = Word( alphas, alphanums + "_" ).setName("identifier")
+ identifier = Word(alphas, alphanums + "_").setName("identifier")
real = Regex(r"[+-]?\d+\.\d*([Ee][+-]?\d+)?").setName("real")
integer = Regex(r"0x[0-9a-fA-F]+|[+-]?\d+").setName("int")
- udTypeName = delimitedList( identifier, "::", combine=True ).setName("udType")
- typeName = ( any_ | boolean_ | char_ | double_ | fixed_ |
- float_ | long_ | octet_ | short_ | string_ |
- wchar_ | wstring_ | udTypeName ).setName("type")
+ udTypeName = delimitedList(identifier, "::", combine=True).setName("udType")
+ 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 ) )
- typedefDef = Group( typedef_ + typeDef + identifier + semi ).setName("typedef")
+ sequenceDef << Group(sequence_ + langle + (sequenceDef | typeName) + rangle)
+ typeDef = sequenceDef | (typeName + Optional(lbrack + integer + rbrack))
+ typedefDef = Group(typedef_ + typeDef + identifier + semi).setName("typedef")
moduleDef = Forward()
- constDef = Group( const_ + typeDef + identifier + equals + ( real | integer | quotedString ) + semi ) #| quotedString )
- exceptionItem = Group( typeDef + identifier + semi )
- exceptionDef = ( exception_ + identifier + lbrace + ZeroOrMore( exceptionItem ) + rbrace + semi )
- attributeDef = Optional( readonly_ ) + attribute_ + typeDef + identifier + semi
- paramlist = delimitedList( Group( ( inout_ | in_ | out_ ) + typeName + identifier ) ).setName( "paramlist" )
- operationDef = ( ( void_ ^ typeDef ) + identifier + lparen + Optional( paramlist ) + rparen + \
- Optional( raises_ + lparen + Group( delimitedList( typeName ) ) + rparen ) + semi )
- interfaceItem = ( constDef | exceptionDef | attributeDef | operationDef )
- interfaceDef = Group( interface_ + identifier + Optional( colon + delimitedList( typeName ) ) + lbrace + \
- ZeroOrMore( interfaceItem ) + rbrace + semi ).setName("opnDef")
- moduleItem = ( interfaceDef | exceptionDef | constDef | typedefDef | moduleDef )
- moduleDef << module_ + identifier + lbrace + ZeroOrMore( moduleItem ) + rbrace + semi
-
- bnf = ( moduleDef | OneOrMore( moduleItem ) )
+ constDef = Group(
+ const_
+ + typeDef
+ + identifier
+ + equals
+ + (real | integer | quotedString)
+ + semi
+ ) # | quotedString )
+ exceptionItem = Group(typeDef + identifier + semi)
+ exceptionDef = (
+ exception_ + identifier + lbrace + ZeroOrMore(exceptionItem) + rbrace + semi
+ )
+ attributeDef = Optional(readonly_) + attribute_ + typeDef + identifier + semi
+ paramlist = delimitedList(
+ Group((inout_ | in_ | out_) + typeName + identifier)
+ ).setName("paramlist")
+ operationDef = (
+ (void_ ^ typeDef)
+ + identifier
+ + lparen
+ + Optional(paramlist)
+ + rparen
+ + Optional(raises_ + lparen + Group(delimitedList(typeName)) + rparen)
+ + semi
+ )
+ interfaceItem = constDef | exceptionDef | attributeDef | operationDef
+ interfaceDef = Group(
+ interface_
+ + identifier
+ + Optional(colon + delimitedList(typeName))
+ + lbrace
+ + ZeroOrMore(interfaceItem)
+ + rbrace
+ + semi
+ ).setName("opnDef")
+ moduleItem = interfaceDef | exceptionDef | constDef | typedefDef | moduleDef
+ moduleDef << module_ + identifier + lbrace + ZeroOrMore(
+ moduleItem
+ ) + rbrace + semi
+
+ bnf = moduleDef | OneOrMore(moduleItem)
singleLineComment = "//" + restOfLine
- bnf.ignore( singleLineComment )
- bnf.ignore( cStyleComment )
+ bnf.ignore(singleLineComment)
+ bnf.ignore(cStyleComment)
return bnf
+
testnum = 1
-def test( strng ):
+
+
+def test(strng):
global testnum
print(strng)
try:
bnf = CORBA_IDL_BNF()
- tokens = bnf.parseString( strng )
+ tokens = bnf.parseString(strng)
print("tokens = ")
- pprint.pprint( tokens.asList() )
+ pprint.pprint(tokens.asList())
imgname = "idlParse%02d.bmp" % testnum
testnum += 1
- #~ tree2image.str2image( str(tokens.asList()), imgname )
+ # ~ tree2image.str2image( str(tokens.asList()), imgname )
except ParseException as err:
print(err.line)
- print(" "*(err.column-1) + "^")
+ print(" " * (err.column - 1) + "^")
print(err)
print()
+
if __name__ == "__main__":
test(
"""
@@ -101,7 +216,7 @@ if __name__ == "__main__":
string method3();
};
"""
- )
+ )
test(
"""
/*
@@ -122,7 +237,7 @@ if __name__ == "__main__":
string method3();
};
"""
- )
+ )
test(
r"""
const string test="Test String\n";
@@ -141,7 +256,7 @@ if __name__ == "__main__":
void method1( in string arg1, inout long arg2 );
};
"""
- )
+ )
test(
"""
module Test1
@@ -158,7 +273,7 @@ if __name__ == "__main__":
};
};
"""
- )
+ )
test(
"""
module Test1
@@ -170,4 +285,4 @@ if __name__ == "__main__":
};
"""
- )
+ )