5 from .cmake
import CMake, Command, CommandGroup, Section, SectionStyle
7 ALL_CAPS = re.compile(
'^[A-Z_]+$')
8 ALL_WHITESPACE = [
'whitespace',
'newline']
9 NOT_REAL = ALL_WHITESPACE + [
'comment']
13 if ALL_CAPS.match(token):
14 return (
'caps', token)
16 return (
'word', token)
19 scanner = re.Scanner([
20 (
r'#.*\n',
lambda scanner, token: (
'comment', token)),
21 (
r'"[^"]*"',
lambda scanner, token: (
'string', token)),
22 (
r'\(',
lambda scanner, token: (
'left paren', token)),
23 (
r'\)',
lambda scanner, token: (
'right paren', token)),
24 (
r'[^ \t\r\n()#"]+', word_cb),
25 (
r'\n',
lambda scanner, token: (
'newline', token)),
26 (
r'[ \t]+',
lambda scanner, token: (
'whitespace', token)),
37 for content
in contents:
39 if content.__class__ == Command
and content.command_name
in [
'if',
'foreach']:
41 depth = base_depth + 1
43 revised_contents.append(content)
45 if content.__class__ == Command:
46 if content.command_name == group.command_name:
48 elif content.command_name ==
'end' + group.command_name:
50 if depth == base_depth:
52 sub = CMake(initial_contents=recursive_contents, depth=base_depth + 1)
53 cg = CommandGroup(group, sub, content)
54 revised_contents.append(cg)
58 current.append(content)
62 revised_contents += current
64 return revised_contents
69 Exception.__init__(self, msg)
74 self.tokens, remainder = scanner.scan(s)
76 msg =
'Unrecognized tokens: %s' % (remainder)
80 for typ, token
in self.tokens:
81 print(
'[%s]%s' % (typ, repr(token)))
84 while len(self.tokens) > 0:
87 self.contents.append(self.
match(typ))
88 elif typ ==
'newline' or typ ==
'whitespace':
90 self.contents.append(s)
91 elif typ
in [
'word',
'caps']:
93 self.contents.append(cmd)
95 raise Exception(
'token ' + typ)
102 print(
'[%s]' % chunk)
105 command_name = self.
match()
106 original = command_name
107 cmd = Command(command_name)
108 while self.
get_type() ==
'whitespace':
109 s = self.
match(
'whitespace')
112 original += self.
match(
'left paren')
115 while len(self.tokens) > 0:
117 if typ
in [
'word',
'caps',
'string']:
119 cmd.sections.append(section)
122 typ, tok_contents = self.tokens.pop(0)
123 original += tok_contents
124 if typ ==
'right paren':
127 cmd.original = original
129 elif typ ==
'left paren':
132 cmd.sections.append(tok_contents)
133 raise CMakeParseError(
'File ended while processing command "%s"' % (command_name))
137 style = SectionStyle()
146 cat = self.
match(
'caps')
148 style.name_val_sep =
'' 149 while self.
get_type()
in ALL_WHITESPACE:
152 style.name_val_sep += s
153 if len(style.name_val_sep) == 0:
154 style.name_val_sep =
' ' 158 while self.
next_real_type()
not in [
'left paren',
'right paren',
'caps']:
160 if typ
in ALL_WHITESPACE:
175 style.val_sep = list(delims)[0]
179 style.val_sep = list(delims)[0]
182 return Section(cat, tokens, style), original
185 if typ
is None or self.
get_type() == typ:
186 typ, tok = self.tokens.pop(0)
190 sys.stderr.write(
'Token Dump:\n')
191 for a
in self.tokens:
192 sys.stderr.write(str(a) +
'\n')
196 if len(self.tokens) > 0:
197 return self.tokens[0][0]
202 for x, y
in self.tokens:
203 if x
not in NOT_REAL:
209 return parser.contents
214 assert len(parser.contents) == 1
215 return parser.contents[0]
219 if not os.path.exists(filename):
221 with open(filename)
as f:
223 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)