Created
April 16, 2017 14:20
-
-
Save guyskk/1630149017f3eaa59c7e30c009741634 to your computer and use it in GitHub Desktop.
validator string DSL parser by pyparsing
This file contains hidden or 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
# https://pythonhosted.org/pyparsing/ | |
from pyparsing import * | |
# https://pyparsing.wikispaces.com/file/view/jsonParser.py | |
def make_keyword(kwd_str, kwd_value): | |
return Keyword(kwd_str).setParseAction(replaceWith(kwd_value)) | |
TRUE = make_keyword("true", True) | |
FALSE = make_keyword("false", False) | |
NULL = make_keyword("null", None) | |
LBRACK, RBRACK, LBRACE, RBRACE, COLON = map(Suppress, "[]{}:") | |
jsonString = dblQuotedString().setParseAction(removeQuotes) | |
jsonNumber = pyparsing_common.number() | |
jsonObject = Forward() | |
jsonValue = Forward() | |
jsonElements = delimitedList( jsonValue ) | |
jsonArray = Group(LBRACK + Optional(jsonElements, []) + RBRACK) | |
jsonValue << (jsonString | jsonNumber | Group(jsonObject) | jsonArray | TRUE | FALSE | NULL) | |
memberDef = Group(jsonString + COLON + jsonValue) | |
jsonMembers = delimitedList(memberDef) | |
jsonObject << Dict(LBRACE + Optional(jsonMembers) + RBRACE) | |
# jsonComment = cppStyleComment | |
# jsonObject.ignore(jsonComment) | |
testdata = """ | |
{ | |
"glossary": { | |
"title": "example glossary", | |
"GlossDiv": { | |
"title": "S", | |
"GlossList": | |
{ | |
"ID": "SGML", | |
"SortAs": "SGML", | |
"GlossTerm": "Standard Generalized Markup Language", | |
"TrueValue": true, | |
"FalseValue": false, | |
"Gravity": -9.8, | |
"LargestPrimeLessThan100": 97, | |
"AvogadroNumber": 6.02E23, | |
"EvenPrimesGreaterThan2": null, | |
"PrimesLessThan10" : [2,3,5,7], | |
"Acronym": "SGML", | |
"Abbrev": "ISO 8879:1986", | |
"GlossDef": "A meta-markup language, used to create markup languages such as DocBook.", | |
"GlossSeeAlso": ["GML", "XML", "markup"], | |
"EmptyDict" : {}, | |
"EmptyList" : [] | |
} | |
} | |
} | |
} | |
""" | |
JSON = jsonValue | |
from beeprint import pp | |
pp(JSON.parseString(testdata).asList(), max_depth=100) | |
KEY = pyparsing_common.identifier | |
VALIDATOR = KEY | |
ARGS = Suppress('(') + Optional(delimitedList(JSON)) + Suppress(')') | |
KW = Group(KEY + Optional(Suppress('=') + JSON)) | |
KWARGS = ZeroOrMore(Suppress('&') + KW) | |
VS = Optional(KEY + Suppress('?')) + VALIDATOR + Group(Optional(ARGS)) + Group(KWARGS) | |
text = """ | |
key?str(0,9, {"k":"v"})&escape=true&optional&desc="hahaha" | |
""" | |
pp(VS.parseString(text).asList()) | |
""" | |
[ | |
'key', | |
'str', | |
[ | |
0, | |
9, | |
[ | |
['k', 'v'], | |
], | |
], | |
[ | |
['escape', True], | |
['optional'], | |
['desc', 'hahaha'], | |
], | |
] | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment