idlParse.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #
  2. # idlparse.py
  3. #
  4. # an example of using the parsing module to be able to process a subset of the CORBA IDL grammar
  5. #
  6. # Copyright (c) 2003, Paul McGuire
  7. #
  8. from pyparsing import Literal, Word, OneOrMore, ZeroOrMore, \
  9. Forward, delimitedList, Group, Optional, alphas, restOfLine, cStyleComment, \
  10. alphanums, quotedString, ParseException, Keyword, Regex
  11. import pprint
  12. #~ import tree2image
  13. bnf = None
  14. def CORBA_IDL_BNF():
  15. global bnf
  16. if not bnf:
  17. # punctuation
  18. (colon,lbrace,rbrace,lbrack,rbrack,lparen,rparen,
  19. equals,comma,dot,slash,bslash,star,semi,langle,rangle) = map(Literal, r":{}[]()=,./\*;<>")
  20. # keywords
  21. (any_, attribute_, boolean_, case_, char_, const_, context_, default_, double_, enum_, exception_,
  22. FALSE_, fixed_, float_, inout_, interface_, in_, long_, module_, Object_, octet_, oneway_, out_, raises_,
  23. readonly_, sequence_, short_, string_, struct_, switch_, TRUE_, typedef_, unsigned_, union_, void_,
  24. wchar_, wstring_) = map(Keyword, """any attribute boolean case char const context
  25. default double enum exception FALSE fixed float inout interface in long module
  26. Object octet oneway out raises readonly sequence short string struct switch
  27. TRUE typedef unsigned union void wchar wstring""".split())
  28. identifier = Word( alphas, alphanums + "_" ).setName("identifier")
  29. real = Regex(r"[+-]?\d+\.\d*([Ee][+-]?\d+)?").setName("real")
  30. integer = Regex(r"0x[0-9a-fA-F]+|[+-]?\d+").setName("int")
  31. udTypeName = delimitedList( identifier, "::", combine=True ).setName("udType")
  32. typeName = ( any_ | boolean_ | char_ | double_ | fixed_ |
  33. float_ | long_ | octet_ | short_ | string_ |
  34. wchar_ | wstring_ | udTypeName ).setName("type")
  35. sequenceDef = Forward().setName("seq")
  36. sequenceDef << Group( sequence_ + langle + ( sequenceDef | typeName ) + rangle )
  37. typeDef = sequenceDef | ( typeName + Optional( lbrack + integer + rbrack ) )
  38. typedefDef = Group( typedef_ + typeDef + identifier + semi ).setName("typedef")
  39. moduleDef = Forward()
  40. constDef = Group( const_ + typeDef + identifier + equals + ( real | integer | quotedString ) + semi ) #| quotedString )
  41. exceptionItem = Group( typeDef + identifier + semi )
  42. exceptionDef = ( exception_ + identifier + lbrace + ZeroOrMore( exceptionItem ) + rbrace + semi )
  43. attributeDef = Optional( readonly_ ) + attribute_ + typeDef + identifier + semi
  44. paramlist = delimitedList( Group( ( inout_ | in_ | out_ ) + typeName + identifier ) ).setName( "paramlist" )
  45. operationDef = ( ( void_ ^ typeDef ) + identifier + lparen + Optional( paramlist ) + rparen + \
  46. Optional( raises_ + lparen + Group( delimitedList( typeName ) ) + rparen ) + semi )
  47. interfaceItem = ( constDef | exceptionDef | attributeDef | operationDef )
  48. interfaceDef = Group( interface_ + identifier + Optional( colon + delimitedList( typeName ) ) + lbrace + \
  49. ZeroOrMore( interfaceItem ) + rbrace + semi ).setName("opnDef")
  50. moduleItem = ( interfaceDef | exceptionDef | constDef | typedefDef | moduleDef )
  51. moduleDef << module_ + identifier + lbrace + ZeroOrMore( moduleItem ) + rbrace + semi
  52. bnf = ( moduleDef | OneOrMore( moduleItem ) )
  53. singleLineComment = "//" + restOfLine
  54. bnf.ignore( singleLineComment )
  55. bnf.ignore( cStyleComment )
  56. return bnf
  57. testnum = 1
  58. def test( strng ):
  59. global testnum
  60. print(strng)
  61. try:
  62. bnf = CORBA_IDL_BNF()
  63. tokens = bnf.parseString( strng )
  64. print("tokens = ")
  65. pprint.pprint( tokens.asList() )
  66. imgname = "idlParse%02d.bmp" % testnum
  67. testnum += 1
  68. #~ tree2image.str2image( str(tokens.asList()), imgname )
  69. except ParseException as err:
  70. print(err.line)
  71. print(" "*(err.column-1) + "^")
  72. print(err)
  73. print()
  74. if __name__ == "__main__":
  75. test(
  76. """
  77. /*
  78. * a block comment *
  79. */
  80. typedef string[10] tenStrings;
  81. typedef sequence<string> stringSeq;
  82. typedef sequence< sequence<string> > stringSeqSeq;
  83. interface QoSAdmin {
  84. stringSeq method1( in string arg1, inout long arg2 );
  85. stringSeqSeq method2( in string arg1, inout long arg2, inout long arg3);
  86. string method3();
  87. };
  88. """
  89. )
  90. test(
  91. """
  92. /*
  93. * a block comment *
  94. */
  95. typedef string[10] tenStrings;
  96. typedef
  97. /** ** *** **** *
  98. * a block comment *
  99. */
  100. sequence<string> /*comment inside an And */ stringSeq;
  101. /* */ /**/ /***/ /****/
  102. typedef sequence< sequence<string> > stringSeqSeq;
  103. interface QoSAdmin {
  104. stringSeq method1( in string arg1, inout long arg2 );
  105. stringSeqSeq method2( in string arg1, inout long arg2, inout long arg3);
  106. string method3();
  107. };
  108. """
  109. )
  110. test(
  111. r"""
  112. const string test="Test String\n";
  113. const long a = 0;
  114. const long b = -100;
  115. const float c = 3.14159;
  116. const long d = 0x007f7f7f;
  117. exception TestException
  118. {
  119. string msg;
  120. sequence<string> dataStrings;
  121. };
  122. interface TestInterface
  123. {
  124. void method1( in string arg1, inout long arg2 );
  125. };
  126. """
  127. )
  128. test(
  129. """
  130. module Test1
  131. {
  132. exception TestException
  133. {
  134. string msg;
  135. ];
  136. interface TestInterface
  137. {
  138. void method1( in string arg1, inout long arg2 )
  139. raises ( TestException );
  140. };
  141. };
  142. """
  143. )
  144. test(
  145. """
  146. module Test1
  147. {
  148. exception TestException
  149. {
  150. string msg;
  151. };
  152. };
  153. """
  154. )