def genParseObject(self,cLocale): sym = DateFormatSymbols(cLocale) val = sym.getEras() frag = Or(CaselessLiteral(val[0].encode('utf-8'))) for item in val[1:]: frag.append(CaselessLiteral(item.encode('utf-8'))) return frag.setResultsName('era')
ddlWord("KEY"), ddlWord("INDEX"), ddlWord("UNIQUE"), ddlWord("CHECK"), ]) ddlColumn = Group(Optional(ddlConstraint).setResultsName( "isConstraint") + OneOrMore(MatchFirst([ ddlUnsigned, ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlFunctionWord("NOW"), ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments, ddlMathCombined, ddlInlineComment ]))) ddlIfNotExists = Optional(Group(ddlWord("IF") + ddlWord("NOT") + ddlWord( "EXISTS")).setResultsName("ifNotExists")) createTable = Group(ddlWord("CREATE") + ddlWord("TABLE") + ddlIfNotExists + ddlName.setResultsName("tableName") + "(" + Group( delimitedList(ddlColumn)).setResultsName( "columns") + ")").setResultsName("create") #ddlString.setDebug(True) #uncomment to debug pyparsing ddl = ZeroOrMore(Suppress(SkipTo(createTable, False)) + createTable) ddlComment = oneOf(["--", "#"]) + restOfLine ddl.ignore(ddlComment) # MAP SQL TYPES types = { 'tinyint': 'tinyint', 'smallint': 'smallint', 'smallserial': 'smallint', # PostgreSQL 'integer': 'integer',
# S -> WHERE C EOF stringLit = Group('"' + w + '"') intLit = Word(nums) C = Forward() T = ((stringLit | intLit | w) + oneOf(_ops) + (stringLit | intLit | w)) | (Suppress("(") + C + Suppress(")")) C << ((T + combinators + T) | T) bvalues = Group(Suppress("(") + values + Suppress(")")) svalues = (bvalues | '*').setResultsName("columns") where_clause = CaselessLiteral("WHERE") + C where = Optional(where_clause, "").setResultsName("where") op = operators.setResultsName("op") sgram = op + svalues + where + StringEnd() insert = CaselessLiteral("INSERT").setResultsName( "op") + bvalues.setResultsName('ivalues') + StringEnd() delete = CaselessLiteral("DELETE").setResultsName("op") + where + StringEnd() stmt = sgram | insert | delete def parse_sql(s): d = {'stmt': s} try: r = stmt.parseString(s) except pyPE, e: raise ParseException(e) o = r.op.lower() if o == "insert":
qualified_identifier = Word(alphas + "_.", alphas + alphanums + "_.") BASIC_TYPES = ["bool", "i8", "u8", "i16", "u16", "i32", "u32", "i64", "u64"] basic_type = Or([Keyword(t) for t in BASIC_TYPES]) bit_width = Word(nums) dec_literal = Word(nums) hex_literal = Combine("0x" + Word(hexnums)) comment = cppStyleComment field = Group( Optional(comment).setResultsName("doc") + identifier.setResultsName("name") + ':' + basic_type.setResultsName("type") + Optional(':' + bit_width.setResultsName("width")) + ';' ) struct = originalTextFor( Group( Optional(comment).setResultsName("doc") + Keyword("struct") + identifier.setResultsName("name") + "{" + OneOrMore(field.setResultsName("fields", listAllMatches=True)) + "}" ), asString=False ) vector = Group( Keyword("vector") + "<" + qualified_identifier.setResultsName("type") + ">"
bom = Regex(BOM) msg_utf8 = bom + utf_8_string msg_any = utf_8_string msg = Combine(Or([msg_utf8, msg_any])).setResultsName("MSG") sd_name = CharsNotIn('= ]"', 1, 32) param_name = sd_name.setResultsName("SD_PARAM_NAME") param_value = QuotedString(quoteChar='"', escChar="\\", multiline=True) param_value = param_value.setResultsName("SD_PARAM_VALUE") sd_id = sd_name.setResultsName("SD_ID") sd_param = Group(param_name + Regex("=") + param_value) sd_params = Group(ZeroOrMore(Group(sp + sd_param.setResultsName("SD_PARAM")))) sd_element = Group("[" + sd_id + sd_params.setResultsName("SD_PARAMS") + "]") sd_element = sd_element.setResultsName("SD_ELEMENT") sd_elements = Group(OneOrMore(sd_element)) structured_data = Or([nilvalue, sd_elements.setResultsName("SD_ELEMENTS")]) structured_data = structured_data.setResultsName("STRUCTURED_DATA") time_hour = Regex("0[0-9]|1[0-9]|2[0-3]") time_minute = Regex("[0-5][0-9]") time_second = time_minute time_secfrac = Regex("\.[0-9]{1,6}") time_numoffset = Or([Regex("\+"), Regex("-")]) + time_hour + ":" + time_minute time_offset = Or([Regex("Z"), time_numoffset]) partial_time = time_hour + ":" + time_minute + ":" + time_second + Optional(time_secfrac) full_time = partial_time + time_offset date_mday = Regex("[0-9]{2}") date_month = Regex("0[1-9]|1[0-2]") date_fullyear = Regex("[0-9]{4}") full_date = date_fullyear + "-" + date_month + "-" + date_mday timestamp = Combine(Or([nilvalue, full_date + "T" + full_time])) timestamp = timestamp.setResultsName("TIMESTAMP") msgid = Or([nilvalue, CharsNotIn('= ]"', 1, 32)])
bom = Regex(BOM) msg_utf8 = bom + utf_8_string msg_any = utf_8_string msg = Combine(Or([msg_utf8, msg_any])).setResultsName('MSG') sd_name = CharsNotIn('= ]"', 1, 32) param_name = sd_name.setResultsName('SD_PARAM_NAME') param_value = QuotedString(quoteChar='"', escChar='\\', multiline=True) param_value = param_value.setResultsName('SD_PARAM_VALUE') sd_id = sd_name.setResultsName('SD_ID') sd_param = Group(param_name + Regex('=') + param_value) sd_params = Group(ZeroOrMore(Group(sp + sd_param.setResultsName('SD_PARAM')))) sd_element = Group('[' + sd_id + sd_params.setResultsName('SD_PARAMS') + ']') sd_element = sd_element.setResultsName('SD_ELEMENT') sd_elements = Group(OneOrMore(sd_element)) structured_data = Or([nilvalue, sd_elements.setResultsName('SD_ELEMENTS')]) structured_data = structured_data.setResultsName('STRUCTURED_DATA') time_hour = Regex('0[0-9]|1[0-9]|2[0-3]') time_minute = Regex('[0-5][0-9]') time_second = time_minute time_secfrac = Regex('\.[0-9]{1,6}') time_numoffset = Or([Regex('\+'), Regex('-')]) + time_hour + ':' + time_minute time_offset = Or([Regex('Z'), time_numoffset]) partial_time = time_hour + ':' + time_minute + ':' + time_second + \ Optional(time_secfrac) full_time = partial_time + time_offset date_mday = Regex('[0-9]{2}') date_month = Regex('0[1-9]|1[0-2]') date_fullyear = Regex('[0-9]{4}') full_date = date_fullyear + '-' + date_month + '-' + date_mday timestamp = Combine(Or([nilvalue, full_date + 'T' + full_time])) timestamp = timestamp.setResultsName('TIMESTAMP')
# 13.1 # DefinitiveOID = "{" + DefinitiveObjIdComponentList + "}" # 13.1 DefinitiveIdentification = Or([ # DefinitiveOID, # DefinitiveOIDandIRI, empty ]) # 13.1 ModuleIdentifier = And([ modulereference, # DefinitiveIdentification ]) # 13.1 ModuleDefinition = And([ ModuleIdentifier.setResultsName("ModuleIdentifier"), DEFINITIONS, EncodingReferenceDefault.setResultsName("EncodingReferenceDefault"), TagDefault.setResultsName("TagDefault"), ExtensionDefault.setResultsName("ExtensionDefault"), assignment, BEGIN, Group(ModuleBody).setResultsName("ModuleBody"), # EncodingControlSection + \ END ]).setParseAction(parse_ModuleDefinition)
bom = Regex(BOM) msg_utf8 = bom + utf_8_string msg_any = utf_8_string msg = Combine(Or([msg_utf8, msg_any])).setResultsName('MSG') sd_name = CharsNotIn('= ]"', 1, 32) param_name = sd_name.setResultsName('SD_PARAM_NAME') param_value = QuotedString(quoteChar='"', escChar='\\', multiline=True) param_value = param_value.setResultsName('SD_PARAM_VALUE') sd_id = sd_name.setResultsName('SD_ID') sd_param = Group(param_name + Regex('=') + param_value) sd_params = Group(ZeroOrMore(Group(sp+sd_param.setResultsName('SD_PARAM')))) sd_element = Group('['+sd_id+sd_params.setResultsName('SD_PARAMS')+']') sd_element = sd_element.setResultsName('SD_ELEMENT') sd_elements = Group(OneOrMore(sd_element)) structured_data = Or([nilvalue, sd_elements.setResultsName('SD_ELEMENTS')]) structured_data = structured_data.setResultsName('STRUCTURED_DATA') time_hour = Regex('0[0-9]|1[0-9]|2[0-3]') time_minute = Regex('[0-5][0-9]') time_second = time_minute time_secfrac = Regex('\.[0-9]{1,6}') time_numoffset = Or([Regex('\+'), Regex('-')]) + \ time_hour + ':' + time_minute time_offset = Or([Regex('Z'), time_numoffset]) partial_time = time_hour + ':' + time_minute + ':' + time_second + \ Optional(time_secfrac) full_time = partial_time + time_offset date_mday = Regex('[0-9]{2}') date_month = Regex('0[1-9]|1[0-2]') date_fullyear = Regex('[0-9]{4}') full_date = date_fullyear + '-' + date_month + '-' + date_mday timestamp = Combine(Or([nilvalue, full_date + 'T' + full_time]))
dec_literal = Word(nums) hex_literal = Combine("0x" + Word(hexnums)) signed_literal = Combine(Optional('-') + (dec_literal ^ hex_literal)) comment = cppStyleComment enumValue = Group( Optional(comment).setResultsName("doc") + identifier.setResultsName("name") + Optional('=' + signed_literal.setResultsName("constant"))) enum = originalTextFor(Group( Optional(comment).setResultsName("doc") + Keyword("enum") + identifier.setResultsName("name") + ':' + basic_type.setResultsName("type") + '{' + delimitedList( enumValue.setResultsName("enum_values", listAllMatches=True), ",") + Optional(',') + '}'), asString=False) field = Group( Optional(comment).setResultsName("doc") + identifier.setResultsName("name") + ':' + qualified_identifier.setResultsName("type") + Optional(':' + bit_width.setResultsName("width")) + ';') struct = originalTextFor(Group( Optional(comment).setResultsName("doc") + Keyword("struct") + identifier.setResultsName("name") + "{" + OneOrMore(field.setResultsName("fields", listAllMatches=True)) + "}"), asString=False)
ddlWord("SMALLSERIAL"), ddlWord("SERIAL"), ddlWord("BIGSERIAL"), ]).setResultsName("hasAutoValue") ddlColumnComment = Group(ddlWord("COMMENT") + ddlString).setResultsName("comment") ddlConstraint = Or([ ddlWord("CONSTRAINT"), ddlWord("PRIMARY"), ddlWord("FOREIGN"), ddlWord("KEY"), ddlWord("INDEX"), ddlWord("UNIQUE"), ]) ddlColumn = Group(Optional(ddlConstraint).setResultsName("isConstraint") + OneOrMore(MatchFirst([ddlNotNull, ddlAutoValue, ddlDefaultValue, ddlFunctionWord("NOW"), ddlTerm, ddlNum, ddlColumnComment, ddlString, ddlArguments]))) ddlIfNotExists = Optional(Group(ddlWord("IF") + ddlWord("NOT") + ddlWord("EXISTS")).setResultsName("ifNotExists")) createTable = Group(ddlWord("CREATE") + ddlWord("TABLE") + ddlIfNotExists + ddlName.setResultsName("tableName") + "(" + Group(delimitedList(ddlColumn)).setResultsName("columns") + ")").setResultsName("create") #ddlString.setDebug(True) #uncomment to debug pyparsing ddl = ZeroOrMore(Suppress(SkipTo(createTable, False)) + createTable) ddlComment = oneOf(["--", "#"]) + restOfLine ddl.ignore(ddlComment) # MAP SQL TYPES types = { 'tinyint': 'tinyint', 'smallint': 'smallint', 'smallserial': 'smallint', # PostgreSQL 'integer': 'integer', 'int': 'integer', 'serial': 'integer', # PostgreSQL