15 from pyparsing
import ( Word, QuotedString, Keyword, Literal, SkipTo, StringEnd,
16 ZeroOrMore, Optional, Combine,
17 alphas, alphanums, printables )
18 dblQuotedString = QuotedString(quoteChar=
'"', escChar=
'\\', unquoteResults=
False)
19 sglQuotedString = QuotedString(quoteChar=
"'", escChar=
'\\', unquoteResults=
False)
20 value = dblQuotedString | sglQuotedString | Word(printables)
22 tag_name = Word(alphas +
"_", alphanums +
"_-")
23 tag_expression = Combine(tag_name + ZeroOrMore(
'&' + tag_name))
24 values = value + ZeroOrMore(tag_expression + value)
26 identifier = Word(alphas +
"_", alphanums +
"_")
27 variable = Combine(identifier +
'=' + value)
29 constituent_option = (Keyword(
'-no_share')
30 | Keyword(
'-no_static')
31 | Keyword(
'-prototypes')
32 | Keyword(
'-no_prototypes')
34 | Keyword(
'-target_tag')
35 | Combine(
'-group=' + value)
36 | Combine(
'-suffix=' + value)
37 | Combine(
'-import=' + value)
40 | Keyword(
'-windows'))
41 source = (Word(alphanums +
"_*./$()")
42 | Combine(
'-s=' + value)
43 | Combine(
'-k=' + value)
44 | Combine(
'-x=' + value))
47 comment = (Literal(
"#") + SkipTo(StringEnd())).suppress()
49 package = Keyword(
'package') + Word(printables)
50 version = Keyword(
"version") + Word(printables)
51 use = Keyword(
"use") + identifier + Word(printables) + Optional(identifier) + Optional(Keyword(
"-no_auto_imports"))
53 constituent = ((Keyword(
'library') | Keyword(
'application') | Keyword(
'document'))
54 + identifier + ZeroOrMore(constituent_option | source))
55 macro = (Keyword(
'macro') | Keyword(
'macro_append')) + identifier + values
56 setenv = (Keyword(
'set') | Keyword(
'path_append') | Keyword(
'path_prepend')) + identifier + values
57 alias = Keyword(
'alias') + identifier + values
59 apply_pattern = Keyword(
"apply_pattern") + identifier + ZeroOrMore(variable)
61 direct_patterns = reduce(operator.or_,
map(Keyword, set(patterns)))
63 direct_patterns.addParseAction(
lambda toks: toks.insert(0,
'apply_pattern'))
64 apply_pattern = apply_pattern | (direct_patterns + ZeroOrMore(variable))
66 statement = (package | version | use | constituent | macro | setenv | alias | apply_pattern)
68 return Optional(statement) + Optional(comment) + StringEnd()