Skip to content

Instantly share code, notes, and snippets.

@aurynn
Created January 22, 2020 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aurynn/dd9b2d652911c97874d6d27a61e86e4c to your computer and use it in GitHub Desktop.
Save aurynn/dd9b2d652911c97874d6d27a61e86e4c to your computer and use it in GitHub Desktop.
from pyparsing import Word, Literal, alphas, alphanums, stringStart, ParserElement, ZeroOrMore,OneOrMore, Forward, Dict, Group, pythonStyleComment, lineStart, lineEnd, FollowedBy, dictOf, oneOf, NotAny, Regex
import re
DEBUG=True
ce = """custom_tooltip = {
fail_text = INVALID_RIVAL_DESC
is_country_type = default
}
"""
annotation_label = ParserElement.suppress(lineStart) + \
ParserElement.suppress(Literal("#")) +\
Word(alphas)
annotation_value = oneOf("= :").suppress() +\
Word(alphas) +\
ParserElement.suppress(lineEnd)
annotation = dictOf(annotation_label, annotation_value)
comment = lineStart.suppress() + pythonStyleComment
label = Word ( alphas + "_" )
# + Literal("{").suppress()
# Defining a name
define = label + Literal ( "=" ).suppress()
block_open = Literal("{").suppress()
block_close = Literal("}").suppress()
# +"<"+"="+">"
# Nested literal?
lit = Word( alphanums + "_" )
lit.setDefaultWhitespaceChars(" \t\n")
# literal = OneOrMore( lit ).setDebug(flag=DEBUG) + lineEnd.setDebug(flag=DEBUG)
# assign_literal = literal.setDebug(flag=DEBUG)
assign_literal = Regex(r"[a-zA-Z_ ]+^")
block = Forward()
exprs = dictOf(define.setDebug(flag=DEBUG), block ^ assign_literal)
block << block_open + OneOrMore(exprs) + block_close
exprs.parseString(ce)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment