def comment(self, parent, cur): """ Comments on any form Args: self (Builder): Code constructor parent (Node): Parent node cur (int): Current position in code Returns: int : End of comment Example: >>> builder = mc.Builder(True, comments=True) >>> builder.load("unnamed", "4 % comment") loading unnamed Program functions.program 0 Main functions.main 0 Codeblock codeblock.codeblock 0 Statement codeblock.codeblock '4' 0 Expression expression.create '4' 0 Int misc.number '4' 2 Comment misc.comment '% comment' >>> builder.configure(suggest=False) >>> print mc.qtree(builder, core=True) # doctest: +NORMALIZE_WHITESPACE 1 1Block code_block TYPE 1 1| Statement code_block TYPE 1 1| | Int int int 1 3| Ecomment code_block TYPE """ assert parent.cls == "Block" if self.code[cur] != "%": self.syntaxerror(cur, "comment") end = findend.comment(self, cur) if not self.comments: return end if self.disp: print "%4d Comment " % cur, print "%-20s" % "misc.comment", print repr(self.code[cur:end]) if self.code[cur + 1] == "{": comment = mc.collection.Bcomment(parent, self.code[cur + 2 : end - 1], cur=cur) else: k = cur - 1 while self.code[k] in " \t": k -= 1 if self.code[k] == "\n": comment = mc.collection.Lcomment(parent, self.code[cur + 1 : end], cur=cur) else: comment = mc.collection.Ecomment(parent, self.code[cur + 1 : end], cur=cur) comment.code = self.code[cur : end + 1] return end
def comment(self, parent, cur): """ Comments on any form Args: self (Builder): Code constructor parent (Node): Parent node cur (int): Current position in code Returns: int : End of comment Example: >>> builder = mc.Builder(True, comments=True) >>> builder.load("unnamed", "4 % comment") loading unnamed Program functions.program 0 Main functions.main 0 Codeblock codeblock.codeblock 0 Statement codeblock.codeblock '4' 0 Expression expression.create '4' 0 Int misc.number '4' 2 Comment misc.comment '% comment' >>> builder.configure(suggest=False) >>> print mc.qtree(builder, core=True) # doctest: +NORMALIZE_WHITESPACE 1 1Block code_block TYPE 1 1| Statement code_block TYPE 1 1| | Int int int 1 3| Ecomment code_block TYPE """ assert parent.cls == "Block" if self.code[cur] != "%": self.syntaxerror(cur, "comment") end = findend.comment(self, cur) if not self.comments: return end if self.disp: print "%4d Comment " % cur, print "%-20s" % "misc.comment", print repr(self.code[cur:end]) if self.code[cur+1] == "{": comment = mc.collection.Bcomment(parent, self.code[cur+2:end-1], cur=cur) else: k = cur-1 while self.code[k] in " \t": k -= 1 if self.code[k] == "\n": comment = mc.collection.Lcomment(parent, self.code[cur+1:end], cur=cur) else: comment = mc.collection.Ecomment(parent, self.code[cur+1:end], cur=cur) comment.code = self.code[cur:end+1] return end
def program(self, name): """ The outer shell of the program Args: self (Builder): Code constructor name (str): Name of the program Returns: Node: The root node of the constructed node tree Example: >>> builder = mc.Builder(True) >>> builder.load("unamed", "a") loading unamed Program functions.program 0 Main functions.main 0 Codeblock codeblock.codeblock 0 Statement codeblock.codeblock 'a' 0 Expression expression.create 'a' 0 Var variables.variable 'a' >>> builder.configure(suggest=False) >>> print mc.qtree(builder) # doctest: +NORMALIZE_WHITESPACE Program program TYPE unamed | Includes program TYPE 1 1| Funcs program TYPE unamed 1 1| | Main func_return TYPE main 1 1| | | Declares func_return TYPE 1 1| | | Returns func_return TYPE 1 1| | | Params func_return TYPE 1 1| | | Block code_block TYPE 1 1| | | | Statement code_block TYPE 1 1| | | | | Var unknown TYPE a | Inlines program TYPE unamed | Structs program TYPE unamed | Headers program TYPE unamed | Log program TYPE unamed """ if self.disp: print " Program ", print "functions.program" # Create intial nodes program = mc.collection.Program(self.project, name=name, cur=0, code=self.code) includes = mc.collection.Includes(program, value=name) funcs = mc.collection.Funcs(program, name=name) mc.collection.Inlines(program, name=name) mc.collection.Structs(program, name=name) mc.collection.Headers(program, name=name) mc.collection.Log(program, name=name) #includes.include("armadillo") #includes.include("mconvert") # Start processing cur = 0 while True: if self.code[cur] in " \t;\n": pass elif self.code[cur] == "%": cur = findend.comment(self, cur) elif self.code[cur:cur+8] == "function": cur = self.create_function(funcs, cur) else: self.create_main(funcs, cur) break if len(self.code)-cur<=2: break cur += 1 #includes.include("namespace_arma") return program
def program(self, name): """ The outer shell of the program Args: self (Builder): Code constructor name (str): Name of the program Returns: Node: The root node of the constructed node tree Example: >>> builder = mc.Builder(True) >>> builder.load("unamed", "a") loading unamed Program functions.program 0 Main functions.main 0 Codeblock codeblock.codeblock 0 Statement codeblock.codeblock 'a' 0 Expression expression.create 'a' 0 Var variables.variable 'a' >>> builder.configure(suggest=False) >>> print mc.qtree(builder) # doctest: +NORMALIZE_WHITESPACE Program program TYPE unamed | Includes program TYPE 1 1| Funcs program TYPE unamed 1 1| | Main func_return TYPE main 1 1| | | Declares func_return TYPE 1 1| | | Returns func_return TYPE 1 1| | | Params func_return TYPE 1 1| | | Block code_block TYPE 1 1| | | | Statement code_block TYPE 1 1| | | | | Var unknown TYPE a | Inlines program TYPE unamed | Structs program TYPE unamed | Headers program TYPE unamed | Log program TYPE unamed """ if self.disp: print " Program ", print "functions.program" # Create intial nodes program = mc.collection.Program(self.project, name=name, cur=0, code=self.code) includes = mc.collection.Includes(program, value=name) funcs = mc.collection.Funcs(program, name=name) mc.collection.Inlines(program, name=name) mc.collection.Structs(program, name=name) mc.collection.Headers(program, name=name) mc.collection.Log(program, name=name) #includes.include("armadillo") #includes.include("mconvert") # Start processing cur = 0 while True: if self.code[cur] in " \t;\n": pass elif self.code[cur] == "%": cur = findend.comment(self, cur) elif self.code[cur:cur + 8] == "function": cur = self.create_function(funcs, cur) else: self.create_main(funcs, cur) break if len(self.code) - cur <= 2: break cur += 1 #includes.include("namespace_arma") return program