Skip to content

Commit

Permalink
[translation] bin/osh_parse compiles and runs with core/ui.py
Browse files Browse the repository at this point in the history
Error messages are printed!  But some of them don't look right.
  • Loading branch information
Andy Chu committed Nov 27, 2019
1 parent 3689091 commit a3cd7a6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
3 changes: 3 additions & 0 deletions build/mycpp.sh
Expand Up @@ -239,6 +239,9 @@ osh-parse-smoke() {
# TODO: Enable this as a separate test of syntax errors
#spec/*) continue ;;

# pgen2 not done
spec/oil-*) continue ;;

# This has Oil syntax
test/oil-runtime-errors.sh) continue ;;
esac
Expand Down
1 change: 1 addition & 0 deletions cpp/asdl_runtime.h
Expand Up @@ -13,6 +13,7 @@ hnode_asdl::hnode__Record* NewRecord(Str* node_type);
hnode_asdl::hnode__Leaf* NewLeaf(Str* s, hnode_asdl::color_t e_color);
extern Str* TRUE_STR;
extern Str* FALSE_STR;
extern int NO_SPID;

} // namespace runtime

Expand Down
41 changes: 37 additions & 4 deletions cpp/core_error.h
Expand Up @@ -5,16 +5,40 @@

#include "mylib.h"

#include "asdl_runtime.h"
#include "syntax_asdl.h"

namespace error {

using syntax_asdl::token;
using syntax_asdl::word_part_t;
using syntax_asdl::word_t;

// This definition is different in Python than C++. Not worth auto-translating.
class _ErrorWithLocation {
public:
_ErrorWithLocation(Str* s, syntax_asdl::token* token)
: token(token) {
_ErrorWithLocation(Str* user_str, int span_id)
: user_str_(user_str),
span_id(span_id), token(nullptr), part(nullptr), word(nullptr) {
}
_ErrorWithLocation(Str* user_str, token* token)
: user_str_(user_str),
span_id(runtime::NO_SPID), token(token), part(nullptr), word(nullptr) {
}
_ErrorWithLocation(Str* user_str, word_part_t* part)
: user_str_(user_str),
span_id(runtime::NO_SPID), token(nullptr), part(part), word(nullptr) {
}
_ErrorWithLocation(Str* user_str, word_t* word)
: user_str_(user_str),
span_id(runtime::NO_SPID), token(nullptr), part(nullptr), word(word) {
}

Str* UserErrorString() {
return user_str_;
}

Str* user_str_;
int span_id;
syntax_asdl::token* token;
syntax_asdl::word_part_t* part;
Expand All @@ -23,8 +47,17 @@ class _ErrorWithLocation {

class Parse : public _ErrorWithLocation {
public:
Parse(Str* s, syntax_asdl::token* token)
: _ErrorWithLocation(s, token) {
Parse(Str* user_str, int span_id)
: _ErrorWithLocation(user_str, span_id) {
}
Parse(Str* user_str, syntax_asdl::token* token)
: _ErrorWithLocation(user_str, token) {
}
Parse(Str* user_str, word_part_t* part)
: _ErrorWithLocation(user_str, part) {
}
Parse(Str* user_str, word_t* word)
: _ErrorWithLocation(user_str, word) {
}
};

Expand Down
10 changes: 5 additions & 5 deletions cpp/preamble.h
Expand Up @@ -36,17 +36,17 @@ Str* repr(void* obj) {
// STUBS for p_die()
// [[noreturn]] avoids warnings
[[noreturn]] void p_die(Str* s, int span_id) {
throw AssertionError();
throw new error::Parse(s, span_id);
}

[[noreturn]] void p_die(Str* s, syntax_asdl::token* blame_token) {
throw AssertionError();
[[noreturn]] void p_die(Str* s, syntax_asdl::token* token) {
throw new error::Parse(s, token);
}

[[noreturn]] void p_die(Str* s, syntax_asdl::word_part_t* part) {
throw AssertionError();
throw new error::Parse(s, part);
}

[[noreturn]] void p_die(Str* s, syntax_asdl::word_t* w) {
throw AssertionError();
throw new error::Parse(s, w);
}
9 changes: 6 additions & 3 deletions mycpp/cppgen_pass.py
Expand Up @@ -1479,12 +1479,13 @@ def visit_func_def(self, o: 'mypy.nodes.FuncDef') -> T:

if (class_name in ('BoolParser', 'CommandParser') and func_name == '_Next' or
class_name == 'ParseContext' and func_name == 'MakeOshParser' or
class_name == 'ErrorFormatter' and func_name == 'PrettyPrintError'
class_name == 'ErrorFormatter' and func_name == 'PrettyPrintError' or
class_name is None and func_name == 'PrettyPrintError'
):

default_val = o.arguments[-1].initializer
if default_val: # e.g. osh/bool_parse.py has default val
if self.decl:
if self.decl or class_name is None:
func_name = o.name()
else:
func_name = '%s::%s' % (self.current_class_name, o.name())
Expand All @@ -1508,7 +1509,9 @@ def visit_func_def(self, o: 'mypy.nodes.FuncDef') -> T:
self.write(' %s%s(' % (kw, o.name()))

# Don't write self or last optional argument
pass_through = o.arguments[1:-1]
first_arg_index = 0 if class_name is None else 1
pass_through = o.arguments[first_arg_index:-1]

if pass_through:
for i, arg in enumerate(pass_through):
if i != 0:
Expand Down
6 changes: 3 additions & 3 deletions spec/12-command-sub.sh
Expand Up @@ -21,14 +21,14 @@ sleep_test() {

g=0

func() {
echo 'running func'
myfunc() {
echo 'running myfunc'
g=1
}

# Hm bash and dash both seem to behave the same here.
var_test() {
func | tee _tmp/command-sub.txt
myfunc | tee _tmp/command-sub.txt
{ g=2; echo brace; } | tee _tmp/command-sub.txt

echo "g after pipeline: $g"
Expand Down

0 comments on commit a3cd7a6

Please sign in to comment.