Beispiel #1
0
Datei: ui.py Projekt: mrshu/oil
def PrintAst(nodes, opts):
  # type: (List[command_t], Any) -> None
  if len(nodes) == 1:
    node = nodes[0]
  else:
    node = command.CommandList(nodes)

  if opts.ast_format == 'none':
    print('AST not printed.', file=sys.stderr)

  else:  # text output
    f = sys.stdout

    if opts.ast_format in ('text', 'abbrev-text'):
      ast_f = fmt.DetectConsoleOutput(f)
    elif opts.ast_format in ('html', 'abbrev-html'):
      ast_f = fmt.HtmlOutput(f)
    else:
      raise AssertionError

    if 'abbrev-' in opts.ast_format:
      tree = node.AbbreviatedTree()
    else:
      tree = node.PrettyTree()

    ast_f.FileHeader()
    fmt.PrintTree(tree, ast_f)
    ast_f.FileFooter()
    ast_f.write('\n')
Beispiel #2
0
    def TeaFunc(self, pnode, out):
        # type: (PNode, command__Func) -> None
        """
    tea_func: (
      '(' [func_params] [';' func_params] ')' [type_expr_list]
      '{' [Op_Newline] [func_items] '}'
    )
    """
        assert pnode.typ == grammar_nt.tea_func
        children = pnode.children
        assert children[0].tok.id == Id.Op_LParen  # proc foo(

        pos = 1
        typ2 = children[pos].typ
        if ISNONTERMINAL(typ2):
            assert typ2 == grammar_nt.func_params, children[pos]  # f(x, y)
            # every other one is a comma
            out.pos_params, out.pos_splat = self._FuncParams(children[pos])
            pos += 1

        id_ = children[pos].tok.id
        if id_ == Id.Op_RParen:  # f()
            pos += 1
        elif id_ == Id.Op_Semi:  # f(; a)
            out.named_params, out.named_splat = self._FuncParams(children[pos +
                                                                          1])
            pos += 3

        if ISNONTERMINAL(children[pos].typ):
            out.return_types = self._TypeExprList(children[pos])
            # otherwise it's Id.Op_LBrace like f() {

        # Stub
        out.body = command.CommandList()
Beispiel #3
0
  def _Suite(self, pnode):
    # type: (PNode) -> command__CommandList
    """
    suite: '{' [Op_Newline] [func_items] '}'
    """
    n = len(pnode.children)

    if n == 2:  # {}
      return command.CommandList([])

    if n == 3:
      if pnode.children[1].typ == grammar_nt.func_items:  # { func_items }
        items_index = 1
      else:
        return command.CommandList([])

    if n == 4:  # { Op_Newline func_items }
      items_index = 2

    return command.CommandList(self.func_items(pnode.children[items_index]))
Beispiel #4
0
def ParseWholeFile(c_parser):
    # type: (CommandParser) -> command_t
    """Parse an entire shell script.

  This uses the same logic as Batch().
  """
    children = []  # type: List[command_t]
    while True:
        node = c_parser.ParseLogicalLine()  # can raise ParseError
        if node is None:  # EOF
            c_parser.CheckForPendingHereDocs()  # can raise ParseError
            break
        children.append(node)

    if len(children) == 1:
        return children[0]
    else:
        return command.CommandList(children)
Beispiel #5
0
def ParseWholeFile(c_parser):
    # type: (CommandParser) -> command_t
    """Parse an entire shell script.

  This uses the same logic as Batch().  Used by:
  - osh -n
  - oshc translate
  - Used by 'trap' to store code.  But 'source' and 'eval' use Batch().
  """
    children = []  # type: List[command_t]
    while True:
        node = c_parser.ParseLogicalLine()  # can raise ParseError
        if node is None:  # EOF
            c_parser.CheckForPendingHereDocs()  # can raise ParseError
            break
        children.append(node)

    if len(children) == 1:
        return children[0]
    else:
        return command.CommandList(children)
Beispiel #6
0
  def PrintAst(nodes, opts):
    # type: (List[command_t], Any) -> None
    if len(nodes) == 1:
      node = nodes[0]
    else:
      node = command.CommandList(nodes)

    if opts.ast_format == 'none':
      print('AST not printed.', file=sys.stderr)
      if 0:
        from _devbuild.gen.id_kind_asdl import Id_str
        from frontend.lexer import ID_HIST
        for id_, count in ID_HIST.most_common(10):
          print('%8d %s' % (count, Id_str(id_)))
        print()
        total = sum(ID_HIST.values())
        print('%8d total tokens returned' % total)

    else:  # text output
      f = mylib.Stdout()

      if opts.ast_format in ('text', 'abbrev-text'):
        ast_f = fmt.DetectConsoleOutput(f)
      elif opts.ast_format in ('html', 'abbrev-html'):
        ast_f = fmt.HtmlOutput(f)
      else:
        raise AssertionError()

      if 'abbrev-' in opts.ast_format:
        tree = node.AbbreviatedTree()
      else:
        tree = node.PrettyTree()

      ast_f.FileHeader()
      fmt.PrintTree(tree, ast_f)
      ast_f.FileFooter()
      ast_f.write('\n')
Beispiel #7
0
 def _Suite(self, pnode):
     # type: (PNode) -> command__CommandList
     return command.CommandList()  # stub