Skip to content

Commit

Permalink
Parse ConstantBytes in the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Thirumalai-Shaktivel committed Jun 25, 2022
1 parent 4591db8 commit 991e3a9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions grammar/Python.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module LPython
| ConstantComplex(float re, float im, string? kind)
| ConstantEllipsis(string? kind)
| ConstantNone(string? kind)
| ConstantBytes(string value, string? kind)

-- the following expression can appear in assignment context
| Attribute(expr value, identifier attr, expr_context ctx)
Expand Down
16 changes: 14 additions & 2 deletions src/lpython/parser/semantics.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,9 @@ char* unescape(Allocator &al, LFortran::Str &s) {
static inline ast_t *PREFIX_STRING(Allocator &al, Location &l, char *prefix, char *s){
Vec<expr_t *> exprs;
exprs.reserve(al, 4);
ast_t *tmp;
ast_t *tmp = nullptr;
// Assuming prefix has only one character.
prefix[0] = tolower(prefix[0]);
if (strcmp(prefix, "f") == 0) {
std::string str = std::string(s);
std::string s1 = "\"";
Expand Down Expand Up @@ -494,8 +496,18 @@ static inline ast_t *PREFIX_STRING(Allocator &al, Location &l, char *prefix, cha
exprs.push_back(al, down_cast<expr_t>(tmp));
}
}
tmp = make_JoinedStr_t(al, l, exprs.p, exprs.size());
} else if (strcmp(prefix, "b") == 0) {
std::string str = std::string(s);
size_t start_pos = 0;
while((start_pos = str.find("\n", start_pos)) != std::string::npos) {
str.replace(start_pos, 1, "\\n");
start_pos += 2;
}
str = "b'" + str + "'";
tmp = make_ConstantBytes_t(al, l, LFortran::s2c(al, str), nullptr);
}
return make_JoinedStr_t(al, l, exprs.p, exprs.size());
return tmp;
}

static inline keyword_t *CALL_KW(Allocator &al, Location &l,
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/lpython_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def visit_Constant(self, node):
new_node = python_ast.ConstantEllipsis(node.kind)
elif isinstance(node.value, None.__class__):
new_node = python_ast.ConstantNone(node.kind)
elif isinstance(node.value, bytes):
new_node = python_ast.ConstantBytes(str(node.value), node.kind)
else:
print(type(node.value))
raise Exception("Unsupported Constant type")
Expand Down

0 comments on commit 991e3a9

Please sign in to comment.