Exemplo n.º 1
0
 def format_error_msg(self, sym, cursor, sub_trees, tok, selection, tokstream):
     if sub_trees:
         cst = sub_trees
     else:
         try:
             cst = self._derive_tree(sym, cursor, sub_trees)[:-1]
         except IncompleteTraceError:
             cst = None
     if cst:
         report = NFAErrorReport(self.langlet, "parse")
         if isinstance(cst[0],list):
             rule = report.error_message(cst[-1], tok, selection)
         else:
             rule = report.error_message(cst, tok, selection)
     else:
         rule = ""
     word = tok[1]
     if tok[1] == '\n':
         word = r"\n"
     line = tokstream.untokenize_current_line().rstrip()
     prefix = "line[%s]: "%tok[2]
     error_line = "\n"+"    \n    "+prefix+"'"+line+"'"+'\n\n'+" "*(tok[3][0]+5+len(prefix))+"^"*(tok[3][1]-tok[3][0])+"\n"
     nmt = "Parser needs more token to finish. " if tok[1] == "<EOF>" else ""
     if self._filename:
         s = "\n\n    "+nmt+"Failed to parse input '%s' at (line %d , column %d) in file:\n\n       %s\n"%(word,tok[2],tok[3][0]+1,self._filename)
         self._filename = None
     else:
         s = nmt+"Failed to parse input '%s' at (line %d , column %d)."%(word,tok[2],tok[3][0]+1)
     terminals = set()
     for item in selection:
         if item in self.terminals:
             terminals.add(item)
         else:
             terminals.update(self.reach[item] & self.terminals)
     return s+error_line+rule+self.format_terminals(terminals)
Exemplo n.º 2
0
 def _handle_error(self, sym, cursor, sub_trees, tok, selection):
     self._error = True
     if sub_trees:
         cst = sub_trees
     else:
         try:
             cst = self._derive_tree(sym, cursor, sub_trees, False)[:-1]
         except IncompleteTraceError:
             cst = None
     if cst:
         nfareport = NFAErrorReport(self.langlet, "lex")
         if isinstance(cst[0], list):
             rule = nfareport.error_message(cst[-1], tok, selection)
         else:
             rule = nfareport.error_message(cst, tok, selection)
     else:
         rule = ""  # TODO: this is unacceptable
     self.dump_error_message(tok, selection, rule)
Exemplo n.º 3
0
 def format_error_msg(self, sym, cursor, sub_trees, tok, selection,
                      tokstream):
     if sub_trees:
         cst = sub_trees
     else:
         try:
             cst = self._derive_tree(sym, cursor, sub_trees)[:-1]
         except IncompleteTraceError:
             cst = None
     if cst:
         report = NFAErrorReport(self.langlet, "parse")
         if isinstance(cst[0], list):
             rule = report.error_message(cst[-1], tok, selection)
         else:
             rule = report.error_message(cst, tok, selection)
     else:
         rule = ""
     word = tok[1]
     if tok[1] == '\n':
         word = r"\n"
     line = tokstream.untokenize_current_line().rstrip()
     prefix = "line[%s]: " % tok[2]
     error_line = "\n" + "    \n    " + prefix + "'" + line + "'" + '\n\n' + " " * (
         tok[3][0] + 5 + len(prefix)) + "^" * (tok[3][1] - tok[3][0]) + "\n"
     nmt = "Parser needs more token to finish. " if tok[1] == "<EOF>" else ""
     if self._filename:
         s = "\n\n    " + nmt + "Failed to parse input '%s' at (line %d , column %d) in file:\n\n       %s\n" % (
             word, tok[2], tok[3][0] + 1, self._filename)
         self._filename = None
     else:
         s = nmt + "Failed to parse input '%s' at (line %d , column %d)." % (
             word, tok[2], tok[3][0] + 1)
     terminals = set()
     for item in selection:
         if item in self.terminals:
             terminals.add(item)
         else:
             terminals.update(self.reach[item] & self.terminals)
     return s + error_line + rule + self.format_terminals(terminals)