Skip to content

Commit

Permalink
[translation] Various fixes / temporary hacks for the parser.
Browse files Browse the repository at this point in the history
- Add dummy catch { } block -- 17 errors left, down from 22
- Add repr(void*) -- 15 errors
- Replace keyword args in CommandParser with Init_*() methods
- Refactor mycpp _Next() hack, so we can generalize it.

Down to 13 compile errors.  Still have to deal properly with TDOP and
exceptions.
  • Loading branch information
Andy Chu committed Nov 19, 2019
1 parent 16a04f9 commit 4f0ff33
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
12 changes: 5 additions & 7 deletions frontend/parse_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,14 @@ def _MakeLexer(self, line_reader):
line_lexer = lexer.LineLexer('', self.arena)
return lexer.Lexer(line_lexer, line_reader)

def MakeOshParser(self, line_reader, emit_comp_dummy=False,
aliases_in_flight=None):
# type: (_Reader, bool, Optional[AliasesInFlight]) -> CommandParser
def MakeOshParser(self, line_reader, emit_comp_dummy=False):
# type: (_Reader, bool) -> CommandParser
lx = self._MakeLexer(line_reader)
if emit_comp_dummy:
lx.EmitCompDummy() # A special token before EOF!

w_parser = word_parse.WordParser(self, lx, line_reader)
c_parser = cmd_parse.CommandParser(self, w_parser, lx, line_reader,
aliases_in_flight=aliases_in_flight)
c_parser = cmd_parse.CommandParser(self, w_parser, lx, line_reader)
return c_parser

def MakeWordParserForHereDoc(self, line_reader):
Expand All @@ -318,8 +316,8 @@ def MakeParserForCommandSub(self, line_reader, lexer, eof_id):
# type: (_Reader, Lexer, Id_t) -> CommandParser
"""To parse command sub, we want a fresh word parser state."""
w_parser = word_parse.WordParser(self, lexer, line_reader)
c_parser = cmd_parse.CommandParser(self, w_parser, lexer, line_reader,
eof_id=eof_id)
c_parser = cmd_parse.CommandParser(self, w_parser, lexer, line_reader)
c_parser.Init_EofId(eof_id)
return c_parser

def MakeWordParserForPlugin(self, code_str):
Expand Down
40 changes: 30 additions & 10 deletions mycpp/cppgen_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,9 +1383,9 @@ def visit_with_stmt(self, o: 'mypy.nodes.WithStmt') -> T:
def visit_del_stmt(self, o: 'mypy.nodes.DelStmt') -> T:
pass

def _write_func_args(self, o: 'mypy.nodes.FuncDef'):
first = True
for i, (arg_type, arg) in enumerate(zip(o.type.arg_types, o.arguments)):
def _write_func_args(self, arg_types, arguments):
first = True # first NOT including self
for arg_type, arg in zip(arg_types, arguments):
if not first:
self.decl_write(', ')

Expand Down Expand Up @@ -1428,6 +1428,13 @@ def visit_func_def(self, o: 'mypy.nodes.FuncDef') -> T:
# if not TYPE_CHECKING: def _Next() # get UnboundType?
# @overload decorator -- not sure how to do it, will probably cause
# runtime overhead

# Have:
# MakeOshParser(_Reader* line_reader, bool emit_comp_dummy)
# Want:
# MakeOshParser(_Reader* line_reader) {
# return MakeOshParser(line_reader, True);
# }
if (self.current_class_name and o.name() == '_Next' and
len(o.arguments) == 2):
default_val = o.arguments[1].initializer
Expand All @@ -1440,14 +1447,19 @@ def visit_func_def(self, o: 'mypy.nodes.FuncDef') -> T:

# Write _Next() with no args
virtual = ''
c_type = get_c_type(o.type.ret_type)
self.decl_write_ind('%s%s %s()', virtual, c_type, func_name)
c_ret_type = get_c_type(o.type.ret_type)
self.decl_write_ind('%s%s %s(', virtual, c_ret_type, func_name)
# TODO: Write all params except optional ones here
self.decl_write(')')
if self.decl:
self.decl_write(';\n')
else:
self.write(' {\n')
self.write(' _Next(')
self.accept(default_val) # e.g. lex_mode_e::DBracket
self.write(' %s(' % o.name())
# TODO: Write all args here

# Now write default value, e.g. lex_mode_e::DBracket
self.accept(default_val)
self.write(');\n')
self.write('}\n')

Expand Down Expand Up @@ -1477,7 +1489,7 @@ def visit_func_def(self, o: 'mypy.nodes.FuncDef') -> T:
c_type = get_c_type(o.type.ret_type)
self.decl_write_ind('%s%s %s(', virtual, c_type, func_name)

self._write_func_args(o)
self._write_func_args(o.type.arg_types, o.arguments)

if self.decl:
self.decl_write(');\n')
Expand Down Expand Up @@ -1561,7 +1573,7 @@ def visit_class_def(self, o: 'mypy.nodes.ClassDef') -> T:
# Constructor is named after class
if isinstance(stmt, FuncDef) and stmt.name() == '__init__':
self.decl_write_ind('%s(', o.name)
self._write_func_args(stmt)
self._write_func_args(stmt.type.arg_types, stmt.arguments)
self.decl_write(');\n')

# Must visit these for member vars!
Expand Down Expand Up @@ -1597,7 +1609,7 @@ def visit_class_def(self, o: 'mypy.nodes.ClassDef') -> T:
if isinstance(stmt, FuncDef) and stmt.name() == '__init__':
self.write('\n')
self.write_ind('%s::%s(', o.name, o.name)
self._write_func_args(stmt)
self._write_func_args(stmt.type.arg_types, stmt.arguments)
self.write(') ')

# Taking into account the docstring, look at the first statement to
Expand Down Expand Up @@ -1890,6 +1902,7 @@ def visit_raise_stmt(self, o: 'mypy.nodes.RaiseStmt') -> T:
def visit_try_stmt(self, o: 'mypy.nodes.TryStmt') -> T:
self.write_ind('try ')
self.accept(o.body)
caught = False
for t, v, handler in zip(o.types, o.vars, o.handlers):

# Heuristic
Expand All @@ -1904,6 +1917,13 @@ def visit_try_stmt(self, o: 'mypy.nodes.TryStmt') -> T:
self.write_ind('catch (%s) ', c_type)
self.accept(handler)

caught = True

# DUMMY to prevent compile errors
# TODO: Remove this
if not caught:
self.write_ind('catch (std::exception) { }')

#if o.else_body:
# raise AssertionError('try/else not supported')
#if o.finally_body:
Expand Down
10 changes: 8 additions & 2 deletions mycpp/examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,11 @@ using id_kind_asdl::Kind_t;
#include "syntax_asdl.h"
#include "types_asdl.h"
// _devbuild/gen-cpp/
// oil/_devbuild/gen-cpp
#include "lookup.h"
#include "grammar_nt.h"
// cpp/
// oil/cpp
#include "pretty.h"
#include "match.h"
Expand All @@ -252,6 +252,11 @@ Str* repr(syntax_asdl::source_t* obj) {
return new Str("TODO");
}
// For hnode::External in asdl/format.py
Str* repr(void* obj) {
return new Str("TODO: repr()");
}
// STUBS for p_die()
void p_die(Str* s, int span_id) {
throw AssertionError();
Expand Down Expand Up @@ -282,6 +287,7 @@ namespace util {
};
}
// for pgen2_demo.py
namespace arith_nt {
const int arith_expr = 1;
}
Expand Down
31 changes: 15 additions & 16 deletions osh/cmd_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,28 +350,18 @@ class CommandParser(object):
word_parse: to get a stream of words
lexer: for lookahead in function def, PushHint of ()
line_reader: for here doc
eof_id: for command subs
arena: where to add nodes, spans, lines, etc.
aliases_in_flight: for preventing infinite alias expansion
"""
def __init__(self,
parse_ctx, # type: ParseContext
w_parser, # type: WordParser
lexer, # type: Lexer
line_reader, # type: _Reader
eof_id=Id.Eof_Real, # type: Id_t
aliases_in_flight=None, # type: Optional[AliasesInFlight]
):
# type: (...) -> None
def __init__(self, parse_ctx, w_parser, lexer, line_reader):
# type: (ParseContext, WordParser, Lexer, _Reader) -> None
self.parse_ctx = parse_ctx
self.aliases = parse_ctx.aliases # aliases to expand at parse time

self.w_parser = w_parser # type: WordParser # for normal parsing
self.lexer = lexer # for pushing hints, lookahead to (
self.line_reader = line_reader # for here docs
self.arena = parse_ctx.arena # for adding here doc and alias spans
self.eof_id = eof_id
self.aliases_in_flight = aliases_in_flight
self.eof_id = Id.Eof_Real
self.aliases_in_flight = None # type: AliasesInFlight

# A hacky boolean to remove 'if cd / {' ambiguity.
self.allow_block = True
Expand All @@ -381,6 +371,15 @@ def __init__(self,

self.Reset()

# These two Init_() functions simulate "keywords args" in C++.
def Init_EofId(self, eof_id):
# type: (Id_t) -> None
self.eof_id = eof_id

def Init_AliasesInFlight(self, aliases_in_flight):
# type: (AliasesInFlight) -> None
self.aliases_in_flight = aliases_in_flight

def Reset(self):
# type: () -> None
"""Reset our own internal state.
Expand Down Expand Up @@ -716,8 +715,8 @@ def _MaybeExpandAliases(self, words):

# NOTE: self.arena isn't correct here. Breaks line invariant.
line_reader = reader.StringLineReader(code_str, self.arena)
cp = self.parse_ctx.MakeOshParser(line_reader,
aliases_in_flight=aliases_in_flight)
cp = self.parse_ctx.MakeOshParser(line_reader)
cp.Init_AliasesInFlight(aliases_in_flight)

# The interaction between COMPLETION and ALIASES requires special care.
# See docstring of BeginAliasExpansion() in parse_lib.py.
Expand Down

0 comments on commit 4f0ff33

Please sign in to comment.