# "THE BEER-WARE LICENSE" (Revision 42): # Mu Security Research Labs wrote this file. As long as you retain this notice # you can do whatever you want with this stuff. If we meet some day, and you # think this stuff is worth it, you can buy us a beer in return. # http://www.musecurity.com # http://labs.musecurity.com class XDR::Parser token IDENTIFIER NUMBER A_NAME rule xdr : definition | xdr definition definition : constant_def | type_def | program_def program_def : 'program' IDENTIFIER '{' program_body '}' '=' NUMBER ';' program_body : version_def | program_body version_def version_def : 'version' IDENTIFIER '{' version_body '}' '=' NUMBER ';' version_body : procedure_def | version_body procedure_def procedure_def : return_def IDENTIFIER '(' argument_def ')' '=' NUMBER ';' return_def : type_specifier | 'void' argument_def : argument_list | 'void' argument_list : type_specifier | argument_list ',' type_specifier constant_def : 'const' IDENTIFIER '=' NUMBER ';' type_def : 'typedef' declaration ';' | 'struct' IDENTIFIER '{' struct_body '}' ';' | 'enum' IDENTIFIER '{' enum_body '}' ';' | 'union' IDENTIFIER union_body ';' declaration : type_specifier IDENTIFIER | type_specifier IDENTIFIER '[' value ']' | type_specifier IDENTIFIER '<' value '>' | type_specifier IDENTIFIER '<' '>' | type_specifier '*' IDENTIFIER | 'opaque' IDENTIFIER '[' value ']' | 'opaque' IDENTIFIER '<' value '>' | 'opaque' IDENTIFIER '<' '>' | 'string' IDENTIFIER '<' value '>' | 'string' IDENTIFIER '<' '>' | 'struct' IDENTIFIER '*' IDENTIFIER | 'struct' '{' struct_body '}' | 'enum' '{' enum_body '}' | 'union' union_body type_specifier : 'unsigned' 'int' | 'unsigned' 'long' | 'unsigned' 'hyper' | 'unsigned' | 'int' | 'long' | 'hyper' | 'float' | 'double' | 'bool' | 'char' | IDENTIFIER value : NUMBER | 'TRUE' | 'FALSE' | IDENTIFIER struct_body : declaration ';' | struct_body declaration ';' enum_body : IDENTIFIER '=' NUMBER | enum_body ',' IDENTIFIER '=' NUMBER union_body : 'switch' '(' declaration ')' '{' switch_body '}' switch_body : switch_case | switch_case switch_default | switch_body switch_case | switch_body switch_case switch_default switch_case : 'case' value ':' case_body ';' | 'case' value ':' switch_default : 'default' ':' case_body ';' case_body : declaration | 'void' ---- inner AUTHOR = "Mu Security" VERSION = 0.1 LICENSE = <<-EOL "THE BEER-WARE LICENSE" (Revision 42): Mu Security Research Labs wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy us a beer in return. http://www.musecurity.com http://labs.musecurity.com EOL def initialize str @str = str end def parse do_parse end RE_ML_COMMENT = /\A\/\*.*?\*\//m RE_COMMENT = /\A#.*\n/ RE_XINCLUDE = /\A%.*\n/ RE_WHITESPACE = /\A\s+/ RE_KEYWORDS = /\A(program|version|const|typedef|enum|struct|switch|union|case|default)\b/ RE_BOOLEAN = /\A(TRUE|FALSE)\b/ RE_TYPES = /\A(opaque|string|void|unsigned|int|long|hyper|float|double|bool|char)\b/ RE_HEX_NUMBER = /\A-?0x[a-fA-F0-9]+/ RE_NUMBER = /\A-?\d+/ RE_IDENTIFIER = /\A[a-zA-Z][a-zA-Z0-9_]*/ RE_MISC = /\A.|\n/o def next_token token = nil while token.nil? and not @str.empty? case @str # multi-line comments, comments and xdr include's when RE_ML_COMMENT when RE_COMMENT when RE_XINCLUDE when RE_WHITESPACE # keywords when RE_KEYWORDS token = [ $&, $& ] # true|false when RE_BOOLEAN token = [ $&, $& ] # types when RE_TYPES token = [ $&, $& ] # base-16 number when RE_HEX_NUMBER token = [ :NUMBER, $&.to_i(0) ] # base-10 number when RE_NUMBER token = [ :NUMBER, $&.to_i ] # identifier when RE_IDENTIFIER token = [ :IDENTIFIER, $& ] # other characters when RE_MISC token = [ $&, $& ] end @str = $' end return token if token return [ false, '$end' ] end ---- footer if __FILE__ == $0 open(ARGV[0]) do |ios| parser = XDR::Parser.new(ios.read) parser.parse end end