class RawBlock(Nary): grammar = ( [ ignore(re.compile('^```$', re.M)), ( ignore(re.compile('^```[ \t]*!:', re.M)), attr('tags', some([' ', Tag])), ) ], attr('content', maybe_some(RawBlockLine)), ignore(re.compile('^```$', re.M)), )
class Block(Nary): grammar = ( [ ignore(re.compile('^````$', re.M)), ( ignore(re.compile('^````[ \t]*!:', re.M)), attr('tags', some([' ', Tag])), ) ], attr('content', some(['\n', BlockLine])), # NOTE: it's ok for a block to eat newlines ignore(re.compile('^````$', re.M)), )
class SelfClosingTag(object): grammar = '<', name(), attr('attributes', Attributes), ignore(whitespace), '/>' def get_name(self): return "'%s'" % self.name def compose(self, parser, indent=0, first=False): text = [] indent = int(indent) indent_str = indent * int(not first) * " " end_indent_str = indent * " " indent_plus_str = (indent + 1) * " " has_contents = bool(self.attributes) paren_sep = '\n' if has_contents else '' contents_sep = ',\n' if has_contents else '' text.append( "{indent}h({paren_sep}{indent_plus}{name}{contents_sep}".format( indent=indent_str, indent_plus=indent_plus_str if has_contents else '', name=self.get_name(), paren_sep=paren_sep, contents_sep=contents_sep, )) text.append( self.attributes.compose(parser, followed_by_children=False, indent=indent + 1)) text.append("{indent})".format( indent=end_indent_str if has_contents else '', )) return ''.join(text)
class ComponentTag(SelfClosingTag): grammar = ('<', attr('name', ComponentName), attr('attributes', Attributes), ignore(whitespace), '/>') def get_name(self): return self.name.compose()
class ComponentTag(SelfClosingTag): """Matches a self closing tag with a name that starts with an uppercase letter. These tags are treating as components and their names are assumed to be Python classes rather than strings. """ grammar = ('<', attr('name', ComponentName), attr('attributes', Attributes), ignore(whitespace), '/>') def get_name(self): return self.name.compose()
class Attributes(List): grammar = optional(ignore(Whitespace), Attribute, maybe_some(ignore(Whitespace), Attribute)) def compose(self, parser, followed_by_children, indent): indent_str = indent * " " if not len(self): indented_paren = '{indent}{{}},\n'.format(indent=indent_str) return indented_paren if followed_by_children else '' text = [] text.append('{indent}{{\n'.format(indent=indent_str)) for entry in self: if not isinstance(entry, str): text.append(entry.compose(parser, indent=indent + 1)) text.append('\n') text.append('{indent}}},\n'.format(indent=indent_str)) return ''.join(text)
class MessageFieldDeclaration: grammar = [ MapType, RepeatedType, PrimitiveType, ], pypeg2.word, '=', pypeg2.ignore(re.compile(r'\d+')), ';', def __init__(self, arguments): self._type = arguments[0] self._name = arguments[1] def get_name(self, sanitized): if sanitized and self._name in FORBIDDEN_WORDS: return self._name + '_' return self._name def get_type(self): return self._type
class ListLine(Nary): grammar = ( attr('indentation', re.compile(r'^(\t| {4})+', re.M)), ignore(re.compile(r'\* *', re.M)), attr('content', some([Inline, ListLineText])), )
class Paragraph(Nary): # Candidate: ^(?!(```|````|\t+\*|( {4})+\*|\|)) # This way, the regex only need to not match [, ] and consecutive \n grammar = (ignore(re.compile(r_paragraph_condition, re.M)), attr('content', some([Inline, ParagraphText])))
class Heading(Nary): grammar = (attr('heading', re.compile(r'#+', re.M)), ignore(re.compile(' *', re.M)), attr('content', some([Inline, HeadingText])))
class BlockLine(Nary): grammar = (ignore(re.compile(r_block_line_condition, re.M)), attr('content', some([Inline, BlockText])))
class Table(Nary): grammar = ( optional(ignore(re.compile(r'^\|[ \t]*!:', re.M)), attr('tags', some([' ', Tag])), '\n'), attr('content', (TableLine, maybe_some('\n', TableLine))), )
class TableLine(Nary): grammar = (ignore(re.compile(r_table_line_condition, re.M)), attr('content', some(['|', TableCell])))
class C1(str): grammar = pypeg2.ignore("!"), pypeg2.restline
class List(Nary): grammar = (ignore(re.compile(r_list_condition, re.M)), optional(ignore(re.compile(r'^(\t| {4})+\*[ \t]*!:', re.M)), attr('tags', some([' ', Tag])), '\n'), attr('content', (ListLine, maybe_some('\n', ListLine))))
class RawBlockLine(Nullary): grammar = (ignore(re.compile(r_rawblock_line_condition, re.M)), attr('content', re.compile(r'^.*\n', re.M)))
attr('content', compound_statement), endl) StructureDeclaration.grammar = (attr( 'documentation', maybe_some([(inline_comment, endl), endline_comment])), 'struct', blank, attr('name', token), endl, '{', endl, attr( 'content', pypeg2.indent( maybe_some(VariableDeclaration, ';', endl))), '}', ';', endl, endl) code = pypeg2.some([ # start with include directives, which can be determined quickly and ignored pypeg2.ignore(include_directive), # next try declarations: they're harder to parse, but we need # to parse them before comments since they have their own comment docs StructureDeclaration, FunctionDeclaration, # last try variable declaration (VariableDeclaration, ';', endl), # next try standalone comments since they're quick to parse inline_comment, endline_comment, ]) scalar_types = ['float', 'int', 'bool'] float_vector_types = [ 'vec2', 'vec3',
print(' private:') print(' const std::shared_ptr<arpc::Channel> channel_;') print('};') print() print( 'static std::unique_ptr<Stub> NewStub(const std::shared_ptr<arpc::Channel>& channel) {' ) print(' return std::make_unique<Stub>(channel);') print('}') print() print('};') ProtoFile = ('syntax', '=', ['"proto3"', '\'proto3\''], ';', 'package', pypeg2.csl(pypeg2.word, separator='.'), ';', pypeg2.ignore((pypeg2.maybe_some(['import', 'option'], pypeg2.restline), )), pypeg2.maybe_some([ EnumDeclaration, MessageDeclaration, ServiceDeclaration, ])) input_str = sys.stdin.read() input_sha256 = hashlib.sha256(input_str.encode('UTF-8')).hexdigest() declarations = pypeg2.parse(input_str, ProtoFile, comment=pypeg2.comment_cpp) package = [] while isinstance(declarations[0], str): package.append(declarations[0]) declarations = declarations[1:] declarations = { declaration.get_name(): declaration