1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
#!/usr/bin/env python3
import typing
import re
import io
class Token:
def __init__(self, lineno: int, tpe: str, text: str):
self.tpe = tpe
self.lineno = lineno
self.text = text
def __str__(self):
return f'Token(tpe={self.tpe}, text={self.text})'
class EmptyLineToken(Token):
def __init__(self, lineno: int):
super().__init__(lineno, 'emptyline', '')
class TextToken(Token):
def __init__(self, lineno: int, indent: int, text: str):
super().__init__(lineno, 'text', text)
self.indent = indent
class LogToken(Token):
def __init__(self, lineno: int, text: str):
super().__init__(lineno, 'log', text)
class UsageToken(Token):
def __init__(self, lineno: int, text: str):
super().__init__(lineno, 'usage', text)
class OptionsToken(Token):
def __init__(self, lineno: int, text: str):
super().__init__(lineno, 'options', text)
class OptionToken(Token):
def __init__(self, lineno: int, text: str):
super().__init__(lineno, 'option', text)
def lexer(lines: typing.Iterator[str]):
for lineno, line in enumerate(lines):
line = line.rstrip()
if not line:
yield EmptyLineToken(lineno)
elif re.match('(?:\033\\[\\d+m)?(?:INFO|ERROR)(?:\033\\[0m)?: ', line):
pass
elif re.match('usage: ', line):
yield UsageToken(lineno, line)
elif re.match('options:', line):
yield OptionsToken(lineno, line)
elif re.match('\\s+-', line):
yield OptionToken(lineno, line)
else:
indent = re.search('\\S', line).start()
yield TextToken(lineno, indent, line)
T = typing.TypeVar('T')
class LookAheadIterator(typing.Generic[T]):
def __init__(self, it: typing.Iterator[T]):
self._it = it
self._lookahead_buffer = list()
self._lookahead_index = -1
def lookahead(self):
if self._lookahead_index + 1 == len(self._lookahead_buffer):
try:
ret = next(self._it)
self._lookahead_index += 1
self._lookahead_buffer.append(ret)
return ret
except StopIteration:
return None
else:
self._lookahead_index += 1
return self._lookahead_buffer[self._lookahead_index]
def consume(self):
self._lookahead_buffer = self._lookahead_buffer[self._lookahead_index+1:]
self._lookahead_index = -1
def rollback(self):
self._lookahead_index -= 1
def reset(self):
self._lookahead_index = -1
class SyntaxErrorException(Exception):
pass
class Parser:
def on_usage(self, text: str):
print('<usage/>')
def enter_description(self):
print('<description>')
def exit_description(self):
print('</description>')
def on_paragraph(self, text: str):
print(f'<paragraph>{text}</paragraph>')
def enter_options(self):
print('<options>')
def exit_options(self):
print('</options>')
def enter_option_group(self, name: str):
print(f'<option_group name={name}>')
def exit_option_group(self, name: str):
print(f'</option_group name={name}>')
def on_option(self, option: str, help: str):
print(f'<option name={option}/>')
def parse_help(self, tokens: LookAheadIterator[Token]):
self.parse_usage(tokens)
self.parse_description(tokens)
self.parse_options(tokens)
tokens.reset()
class MatchFailure(Exception):
pass
def parse_usage(self, tokens: LookAheadIterator[Token]):
token = tokens.lookahead()
self.assert_token_tpe('usage', token)
buf = io.StringIO()
buf.write(token.text)
while True:
token = tokens.lookahead()
if token is None:
tokens.consume()
self.on_usage(buf.getvalue())
elif token.tpe == 'text':
buf.write(' ')
buf.write(token.text[token.indent:])
elif token.tpe == 'option':
buf.write(' ')
buf.write(token.text.lstrip())
else:
self.assert_token_tpe('emptyline', token)
tokens.rollback()
self.parse_emptyline(tokens)
self.on_usage(buf.getvalue())
break
def parse_description(self, tokens: LookAheadIterator[Token]):
result = self.parse_paragraph(tokens)
if result is not None:
indent, paragraph = result
self.enter_description()
tokens.consume()
self.on_paragraph(paragraph)
while True:
ret = self.parse_paragraph(tokens)
if ret is None:
self.exit_description()
return
indent, text = ret
tokens.consume()
self.on_paragraph(text)
def parse_options(self, tokens: LookAheadIterator[Token]):
token = tokens.lookahead()
if token.tpe != 'options':
return
else:
self.enter_options()
self.parse_option_group(tokens)
while self.parse_named_option_group(tokens):
pass
self.exit_options()
def parse_option_group(self, tokens: LookAheadIterator[Token]):
while True:
ret = self.parse_option(tokens)
if ret is None:
break
option, help = ret
self.on_option(option, help)
def parse_option(self, tokens: LookAheadIterator[Token]):
token = tokens.lookahead()
if token is not None and token.tpe == 'option':
match = re.match(
'\\s+((?:-[a-zA-Z]|--[-a-zA-Z0-9]+)(?:\\s[^, ]+)?(?:,\\s+--[-a-zA-Z0-9]+(?:\\s[^, ]+)?)?)\\s*',
token.text,
)
if match is None:
tokens.rollback()
return None
option = match.group(1)
buf = io.StringIO()
buf.write(token.text[match.end():])
while True:
token = tokens.lookahead()
if token is None:
tokens.consume()
break
elif token.tpe != 'text':
tokens.rollback()
break
else:
tokens.consume()
if buf.tell() != 0:
buf.write(' ')
buf.write(token.text[token.indent:])
return option, buf.getvalue()
def parse_named_option_group(self, tokens: LookAheadIterator[Token]):
token = tokens.lookahead()
if token is None:
tokens.rollback()
return
elif token.tpe == 'emptyline':
tokens.rollback()
self.parse_emptyline(tokens)
if token.tpe == 'text' and token.indent == 0 and token.text[-1] == ':':
group_name = token.text[:-1]
description = list()
while True:
ret = self.parse_paragraph(tokens)
if ret is None:
break
indent, text = ret
description.append(text)
ret = self.parse_option(tokens)
if ret is None:
tokens.reset()
return
else:
self.enter_option_group(group_name)
for paragraph in description:
self.on_paragraph(paragraph)
option, help = ret
self.on_option(option, help)
tokens.consume()
while True:
ret = self.parse_option(tokens)
if ret is None:
break
option, help = ret
self.on_option(option, help)
tokens.consume()
self.parse_emptyline(tokens)
self.exit_option_group(group_name)
tokens.consume()
return True
else:
tokens.rollback()
return False
@classmethod
def assert_token_tpe(cls, tpe: str, token: Token):
if token.tpe != tpe:
raise SyntaxErrorException(f'Expected: <{tpe}>. Actual: <{token.tpe}>')
def parse_log(self, tokens: LookAheadIterator[Token]):
# TODO: remove me
while True:
token = tokens.lookahead()
if token.tpe == 'log':
tokens.consume()
else:
return
def parse_paragraph(self, tokens: LookAheadIterator[Token]):
buf = io.StringIO()
indent = -1
while True:
token = tokens.lookahead()
if token is None:
tokens.consume()
break
if token.tpe != 'text':
tokens.rollback()
break
new_indent = token.indent
if indent == -1:
indent = new_indent
buf.write(token.text[indent:])
elif new_indent == indent:
buf.write(' ')
buf.write(token.text[indent:])
else:
tokens.rollback()
break
if indent == -1:
return None
else:
token = tokens.lookahead()
if token is not None:
tokens.rollback()
self.parse_emptyline(tokens)
return indent, buf.getvalue()
def parse_emptyline(self, tokens: LookAheadIterator[Token]):
while True:
token = tokens.lookahead()
if token.tpe != 'emptyline':
tokens.rollback()
break
def main():
import sys
stdin = sys.stdin
with open(3, buffering=1) as stdin:
tokens = LookAheadIterator(lexer(stdin))
Parser().parse_help(tokens)
print('epilog starts at line {}'.format(tokens.lookahead().lineno))
if __name__ == '__main__':
main()
|