3 from cmake
import CMake, Command, Section, SectionStyle, CommandGroup
5 ALL_CAPS = re.compile(
'^[A-Z_]+$')
6 ALL_WHITESPACE = [
'whitespace',
'newline']
7 NOT_REAL = ALL_WHITESPACE + [
'comment']
11 if ALL_CAPS.match(token):
12 return (
'caps', token)
14 return (
'word', token)
17 scanner = re.Scanner([
18 (
r'#.*\n',
lambda scanner, token: (
"comment", token)),
19 (
r'"[^"]*"',
lambda scanner, token: (
"string", token)),
20 (
r"\(",
lambda scanner, token: (
"left paren", token)),
21 (
r"\)",
lambda scanner, token: (
"right paren", token)),
22 (
r'[^ \t\r\n()#"]+', word_cb),
23 (
r'\n',
lambda scanner, token: (
"newline", token)),
24 (
r"[ \t]+",
lambda scanner, token: (
"whitespace", token)),
35 for content
in contents:
37 if content.__class__ == Command
and content.command_name
in [
'if',
'foreach']:
39 depth = base_depth + 1
41 revised_contents.append(content)
43 if content.__class__ == Command:
44 if content.command_name == group.command_name:
46 elif content.command_name ==
'end' + group.command_name:
48 if depth == base_depth:
50 sub = CMake(initial_contents=recursive_contents, depth=base_depth + 1)
51 cg = CommandGroup(group, sub, content)
52 revised_contents.append(cg)
56 current.append(content)
60 revised_contents += current
62 return revised_contents
67 Exception.__init__(self, msg)
72 self.tokens, remainder = scanner.scan(s)
74 msg =
'Unrecognized tokens: %s' % (remainder)
78 for typ, token
in self.tokens:
79 print(
'[%s]%s' % (typ, repr(token)))
82 while len(self.tokens) > 0:
85 self.contents.append(self.
match(typ))
86 elif typ ==
'newline' or typ ==
'whitespace':
88 self.contents.append(s)
89 elif typ
in [
'word',
'caps']:
91 self.contents.append(cmd)
93 raise Exception(
'token ' + typ)
100 print(
'[%s]' % chunk)
103 command_name = self.
match()
104 original = command_name
105 cmd = Command(command_name)
106 while self.
get_type() ==
'whitespace':
107 s = self.
match(
'whitespace')
110 original += self.
match(
'left paren')
113 while len(self.tokens) > 0:
115 if typ
in [
'word',
'caps',
'string']:
117 cmd.sections.append(section)
120 typ, tok_contents = self.tokens.pop(0)
121 original += tok_contents
122 if typ ==
'right paren':
125 cmd.original = original
127 elif typ ==
'left paren':
130 cmd.sections.append(tok_contents)
131 raise CMakeParseError(
'File ended while processing command "%s"' % (command_name))
135 style = SectionStyle()
144 cat = self.
match(
'caps')
146 style.name_val_sep =
'' 147 while self.
get_type()
in ALL_WHITESPACE:
150 style.name_val_sep += s
151 if len(style.name_val_sep) == 0:
152 style.name_val_sep =
' ' 156 while self.
next_real_type()
not in [
'left paren',
'right paren',
'caps']:
158 if typ
in ALL_WHITESPACE:
173 style.val_sep = list(delims)[0]
177 style.val_sep = list(delims)[0]
180 return Section(cat, tokens, style), original
183 if typ
is None or self.
get_type() == typ:
184 typ, tok = self.tokens.pop(0)
188 sys.stderr.write(
'Token Dump:\n')
189 for a
in self.tokens:
190 sys.stderr.write(str(a) +
'\n')
194 if len(self.tokens) > 0:
195 return self.tokens[0][0]
200 for x, y
in self.tokens:
201 if x
not in NOT_REAL:
207 return parser.contents
212 assert len(parser.contents) == 1
213 return parser.contents[0]
217 with open(filename)
as f:
219 return CMake(file_path=filename, initial_contents=
parse_commands(s))
def match(self, typ=None)
def word_cb(scanner, token)
def __init__(self, s, debug=False)
def match_command_groups(contents, base_depth=0)