natalie_parser 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +22 -0
- data/Dockerfile +26 -0
- data/Gemfile +10 -0
- data/LICENSE +21 -0
- data/README.md +55 -0
- data/Rakefile +242 -0
- data/ext/natalie_parser/extconf.rb +9 -0
- data/ext/natalie_parser/mri_creator.hpp +139 -0
- data/ext/natalie_parser/natalie_parser.cpp +144 -0
- data/include/natalie_parser/creator/debug_creator.hpp +113 -0
- data/include/natalie_parser/creator.hpp +108 -0
- data/include/natalie_parser/lexer/interpolated_string_lexer.hpp +64 -0
- data/include/natalie_parser/lexer/regexp_lexer.hpp +37 -0
- data/include/natalie_parser/lexer/word_array_lexer.hpp +57 -0
- data/include/natalie_parser/lexer.hpp +135 -0
- data/include/natalie_parser/node/alias_node.hpp +35 -0
- data/include/natalie_parser/node/arg_node.hpp +74 -0
- data/include/natalie_parser/node/array_node.hpp +34 -0
- data/include/natalie_parser/node/array_pattern_node.hpp +28 -0
- data/include/natalie_parser/node/assignment_node.hpp +34 -0
- data/include/natalie_parser/node/back_ref_node.hpp +28 -0
- data/include/natalie_parser/node/begin_block_node.hpp +25 -0
- data/include/natalie_parser/node/begin_node.hpp +52 -0
- data/include/natalie_parser/node/begin_rescue_node.hpp +47 -0
- data/include/natalie_parser/node/bignum_node.hpp +37 -0
- data/include/natalie_parser/node/block_node.hpp +55 -0
- data/include/natalie_parser/node/block_pass_node.hpp +33 -0
- data/include/natalie_parser/node/break_node.hpp +32 -0
- data/include/natalie_parser/node/call_node.hpp +85 -0
- data/include/natalie_parser/node/case_in_node.hpp +40 -0
- data/include/natalie_parser/node/case_node.hpp +52 -0
- data/include/natalie_parser/node/case_when_node.hpp +43 -0
- data/include/natalie_parser/node/class_node.hpp +39 -0
- data/include/natalie_parser/node/colon2_node.hpp +44 -0
- data/include/natalie_parser/node/colon3_node.hpp +34 -0
- data/include/natalie_parser/node/constant_node.hpp +26 -0
- data/include/natalie_parser/node/def_node.hpp +55 -0
- data/include/natalie_parser/node/defined_node.hpp +33 -0
- data/include/natalie_parser/node/encoding_node.hpp +26 -0
- data/include/natalie_parser/node/end_block_node.hpp +25 -0
- data/include/natalie_parser/node/evaluate_to_string_node.hpp +37 -0
- data/include/natalie_parser/node/false_node.hpp +23 -0
- data/include/natalie_parser/node/fixnum_node.hpp +36 -0
- data/include/natalie_parser/node/float_node.hpp +36 -0
- data/include/natalie_parser/node/hash_node.hpp +34 -0
- data/include/natalie_parser/node/hash_pattern_node.hpp +27 -0
- data/include/natalie_parser/node/identifier_node.hpp +123 -0
- data/include/natalie_parser/node/if_node.hpp +43 -0
- data/include/natalie_parser/node/infix_op_node.hpp +46 -0
- data/include/natalie_parser/node/interpolated_node.hpp +33 -0
- data/include/natalie_parser/node/interpolated_regexp_node.hpp +28 -0
- data/include/natalie_parser/node/interpolated_shell_node.hpp +22 -0
- data/include/natalie_parser/node/interpolated_string_node.hpp +31 -0
- data/include/natalie_parser/node/interpolated_symbol_key_node.hpp +18 -0
- data/include/natalie_parser/node/interpolated_symbol_node.hpp +28 -0
- data/include/natalie_parser/node/iter_node.hpp +45 -0
- data/include/natalie_parser/node/keyword_arg_node.hpp +25 -0
- data/include/natalie_parser/node/keyword_splat_node.hpp +38 -0
- data/include/natalie_parser/node/logical_and_node.hpp +40 -0
- data/include/natalie_parser/node/logical_or_node.hpp +40 -0
- data/include/natalie_parser/node/match_node.hpp +38 -0
- data/include/natalie_parser/node/module_node.hpp +32 -0
- data/include/natalie_parser/node/multiple_assignment_arg_node.hpp +32 -0
- data/include/natalie_parser/node/multiple_assignment_node.hpp +37 -0
- data/include/natalie_parser/node/next_node.hpp +37 -0
- data/include/natalie_parser/node/nil_node.hpp +23 -0
- data/include/natalie_parser/node/nil_sexp_node.hpp +23 -0
- data/include/natalie_parser/node/node.hpp +155 -0
- data/include/natalie_parser/node/node_with_args.hpp +47 -0
- data/include/natalie_parser/node/not_match_node.hpp +35 -0
- data/include/natalie_parser/node/not_node.hpp +37 -0
- data/include/natalie_parser/node/nth_ref_node.hpp +27 -0
- data/include/natalie_parser/node/op_assign_accessor_node.hpp +74 -0
- data/include/natalie_parser/node/op_assign_and_node.hpp +34 -0
- data/include/natalie_parser/node/op_assign_node.hpp +47 -0
- data/include/natalie_parser/node/op_assign_or_node.hpp +34 -0
- data/include/natalie_parser/node/pin_node.hpp +33 -0
- data/include/natalie_parser/node/range_node.hpp +52 -0
- data/include/natalie_parser/node/redo_node.hpp +20 -0
- data/include/natalie_parser/node/regexp_node.hpp +36 -0
- data/include/natalie_parser/node/retry_node.hpp +20 -0
- data/include/natalie_parser/node/return_node.hpp +34 -0
- data/include/natalie_parser/node/safe_call_node.hpp +31 -0
- data/include/natalie_parser/node/sclass_node.hpp +37 -0
- data/include/natalie_parser/node/self_node.hpp +23 -0
- data/include/natalie_parser/node/shadow_arg_node.hpp +40 -0
- data/include/natalie_parser/node/shell_node.hpp +32 -0
- data/include/natalie_parser/node/splat_node.hpp +39 -0
- data/include/natalie_parser/node/splat_value_node.hpp +32 -0
- data/include/natalie_parser/node/stabby_proc_node.hpp +29 -0
- data/include/natalie_parser/node/string_node.hpp +42 -0
- data/include/natalie_parser/node/super_node.hpp +44 -0
- data/include/natalie_parser/node/symbol_key_node.hpp +19 -0
- data/include/natalie_parser/node/symbol_node.hpp +30 -0
- data/include/natalie_parser/node/to_array_node.hpp +33 -0
- data/include/natalie_parser/node/true_node.hpp +23 -0
- data/include/natalie_parser/node/unary_op_node.hpp +41 -0
- data/include/natalie_parser/node/undef_node.hpp +31 -0
- data/include/natalie_parser/node/until_node.hpp +21 -0
- data/include/natalie_parser/node/while_node.hpp +52 -0
- data/include/natalie_parser/node/yield_node.hpp +29 -0
- data/include/natalie_parser/node.hpp +89 -0
- data/include/natalie_parser/parser.hpp +218 -0
- data/include/natalie_parser/token.hpp +842 -0
- data/include/tm/defer.hpp +34 -0
- data/include/tm/hashmap.hpp +826 -0
- data/include/tm/macros.hpp +16 -0
- data/include/tm/optional.hpp +223 -0
- data/include/tm/owned_ptr.hpp +186 -0
- data/include/tm/recursion_guard.hpp +156 -0
- data/include/tm/shared_ptr.hpp +259 -0
- data/include/tm/string.hpp +1447 -0
- data/include/tm/tests.hpp +78 -0
- data/include/tm/vector.hpp +796 -0
- data/lib/natalie_parser/sexp.rb +36 -0
- data/lib/natalie_parser/version.rb +5 -0
- data/lib/natalie_parser.rb +3 -0
- data/natalie_parser.gemspec +23 -0
- data/src/lexer/interpolated_string_lexer.cpp +88 -0
- data/src/lexer/regexp_lexer.cpp +95 -0
- data/src/lexer/word_array_lexer.cpp +134 -0
- data/src/lexer.cpp +1703 -0
- data/src/node/alias_node.cpp +11 -0
- data/src/node/assignment_node.cpp +33 -0
- data/src/node/begin_node.cpp +29 -0
- data/src/node/begin_rescue_node.cpp +33 -0
- data/src/node/class_node.cpp +22 -0
- data/src/node/interpolated_regexp_node.cpp +19 -0
- data/src/node/interpolated_shell_node.cpp +25 -0
- data/src/node/interpolated_string_node.cpp +111 -0
- data/src/node/interpolated_symbol_node.cpp +25 -0
- data/src/node/match_node.cpp +14 -0
- data/src/node/module_node.cpp +21 -0
- data/src/node/multiple_assignment_node.cpp +37 -0
- data/src/node/node.cpp +10 -0
- data/src/node/node_with_args.cpp +35 -0
- data/src/node/op_assign_node.cpp +36 -0
- data/src/node/string_node.cpp +33 -0
- data/src/parser.cpp +2972 -0
- data/src/token.cpp +27 -0
- metadata +186 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
5
|
+
#include "tm/hashmap.hpp"
|
|
6
|
+
#include "tm/string.hpp"
|
|
7
|
+
|
|
8
|
+
namespace NatalieParser {
|
|
9
|
+
|
|
10
|
+
using namespace TM;
|
|
11
|
+
|
|
12
|
+
class ConstantNode : public Node {
|
|
13
|
+
public:
|
|
14
|
+
ConstantNode(const Token &token)
|
|
15
|
+
: Node { token } { }
|
|
16
|
+
|
|
17
|
+
virtual Type type() const override { return Type::Constant; }
|
|
18
|
+
|
|
19
|
+
SharedPtr<String> name() const { return m_token.literal_string(); }
|
|
20
|
+
|
|
21
|
+
virtual void transform(Creator *creator) const override {
|
|
22
|
+
creator->set_type("const");
|
|
23
|
+
creator->append_symbol(name());
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/block_node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node.hpp"
|
|
5
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
6
|
+
#include "tm/hashmap.hpp"
|
|
7
|
+
#include "tm/owned_ptr.hpp"
|
|
8
|
+
#include "tm/string.hpp"
|
|
9
|
+
|
|
10
|
+
namespace NatalieParser {
|
|
11
|
+
|
|
12
|
+
using namespace TM;
|
|
13
|
+
|
|
14
|
+
class DefNode : public NodeWithArgs {
|
|
15
|
+
public:
|
|
16
|
+
DefNode(const Token &token, SharedPtr<Node> self_node, SharedPtr<String> name, const Vector<SharedPtr<Node>> &args, SharedPtr<BlockNode> body)
|
|
17
|
+
: NodeWithArgs { token, args }
|
|
18
|
+
, m_self_node { self_node }
|
|
19
|
+
, m_name { name }
|
|
20
|
+
, m_body { body } {
|
|
21
|
+
assert(m_body);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
virtual Type type() const override { return Type::Def; }
|
|
25
|
+
|
|
26
|
+
const SharedPtr<Node> self_node() const { return m_self_node; }
|
|
27
|
+
SharedPtr<String> name() const { return m_name; }
|
|
28
|
+
const SharedPtr<BlockNode> body() const { return m_body; }
|
|
29
|
+
|
|
30
|
+
virtual void transform(Creator *creator) const override {
|
|
31
|
+
if (m_self_node) {
|
|
32
|
+
creator->set_type("defs");
|
|
33
|
+
creator->append(m_self_node.ref());
|
|
34
|
+
} else {
|
|
35
|
+
creator->set_type("defn");
|
|
36
|
+
}
|
|
37
|
+
auto doc_comment = doc();
|
|
38
|
+
if (doc_comment)
|
|
39
|
+
creator->set_comments(doc_comment.value().ref());
|
|
40
|
+
creator->append_symbol(m_name);
|
|
41
|
+
append_method_or_block_args(creator);
|
|
42
|
+
if (m_body->is_empty()) {
|
|
43
|
+
creator->append_nil_sexp();
|
|
44
|
+
} else {
|
|
45
|
+
for (auto node : m_body->nodes())
|
|
46
|
+
creator->append(node);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
protected:
|
|
51
|
+
SharedPtr<Node> m_self_node {};
|
|
52
|
+
SharedPtr<String> m_name {};
|
|
53
|
+
SharedPtr<BlockNode> m_body {};
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
5
|
+
#include "tm/hashmap.hpp"
|
|
6
|
+
#include "tm/owned_ptr.hpp"
|
|
7
|
+
#include "tm/string.hpp"
|
|
8
|
+
|
|
9
|
+
namespace NatalieParser {
|
|
10
|
+
|
|
11
|
+
using namespace TM;
|
|
12
|
+
|
|
13
|
+
class DefinedNode : public Node {
|
|
14
|
+
public:
|
|
15
|
+
DefinedNode(const Token &token, SharedPtr<Node> arg)
|
|
16
|
+
: Node { token }
|
|
17
|
+
, m_arg { arg } {
|
|
18
|
+
assert(arg);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
virtual Type type() const override { return Type::Defined; }
|
|
22
|
+
|
|
23
|
+
const SharedPtr<Node> arg() const { return m_arg; }
|
|
24
|
+
|
|
25
|
+
virtual void transform(Creator *creator) const override {
|
|
26
|
+
creator->set_type("defined");
|
|
27
|
+
creator->append(m_arg.ref());
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected:
|
|
31
|
+
SharedPtr<Node> m_arg {};
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
|
|
5
|
+
namespace NatalieParser {
|
|
6
|
+
|
|
7
|
+
using namespace TM;
|
|
8
|
+
|
|
9
|
+
class EncodingNode : public Node {
|
|
10
|
+
public:
|
|
11
|
+
EncodingNode(const Token &token)
|
|
12
|
+
: Node { token } { }
|
|
13
|
+
|
|
14
|
+
virtual Type type() const override { return Type::Encoding; }
|
|
15
|
+
|
|
16
|
+
virtual void transform(Creator *creator) const override {
|
|
17
|
+
// s(:colon2, s(:const, :Encoding), :UTF_8)
|
|
18
|
+
creator->set_type("colon2");
|
|
19
|
+
creator->append_sexp([&](Creator *c) {
|
|
20
|
+
c->set_type("const");
|
|
21
|
+
c->append_symbol("Encoding");
|
|
22
|
+
});
|
|
23
|
+
creator->append_symbol("UTF_8"); // TODO: support file-encodings other than UTF-8
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
5
|
+
#include "tm/hashmap.hpp"
|
|
6
|
+
#include "tm/string.hpp"
|
|
7
|
+
|
|
8
|
+
namespace NatalieParser {
|
|
9
|
+
|
|
10
|
+
using namespace TM;
|
|
11
|
+
|
|
12
|
+
class EndBlockNode : public Node {
|
|
13
|
+
public:
|
|
14
|
+
EndBlockNode(const Token &token)
|
|
15
|
+
: Node { token } { }
|
|
16
|
+
|
|
17
|
+
virtual Type type() const override { return Type::EndBlock; }
|
|
18
|
+
|
|
19
|
+
virtual bool can_accept_a_block() const override { return true; }
|
|
20
|
+
|
|
21
|
+
virtual void transform(Creator *creator) const override {
|
|
22
|
+
creator->set_type("postexe");
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
5
|
+
#include "tm/hashmap.hpp"
|
|
6
|
+
#include "tm/owned_ptr.hpp"
|
|
7
|
+
#include "tm/string.hpp"
|
|
8
|
+
|
|
9
|
+
namespace NatalieParser {
|
|
10
|
+
|
|
11
|
+
using namespace TM;
|
|
12
|
+
|
|
13
|
+
class EvaluateToStringNode : public Node {
|
|
14
|
+
public:
|
|
15
|
+
EvaluateToStringNode(const Token &token)
|
|
16
|
+
: Node { token } { }
|
|
17
|
+
|
|
18
|
+
EvaluateToStringNode(const Token &token, SharedPtr<Node> node)
|
|
19
|
+
: Node { token }
|
|
20
|
+
, m_node { node } {
|
|
21
|
+
assert(m_node);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
virtual Type type() const override { return Type::EvaluateToString; }
|
|
25
|
+
|
|
26
|
+
const SharedPtr<Node> node() const { return m_node; }
|
|
27
|
+
|
|
28
|
+
virtual void transform(Creator *creator) const override {
|
|
29
|
+
creator->set_type("evstr");
|
|
30
|
+
if (m_node)
|
|
31
|
+
creator->append(m_node);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
protected:
|
|
35
|
+
SharedPtr<Node> m_node {};
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
5
|
+
#include "tm/hashmap.hpp"
|
|
6
|
+
#include "tm/string.hpp"
|
|
7
|
+
|
|
8
|
+
namespace NatalieParser {
|
|
9
|
+
|
|
10
|
+
using namespace TM;
|
|
11
|
+
|
|
12
|
+
class FalseNode : public Node {
|
|
13
|
+
public:
|
|
14
|
+
FalseNode(const Token &token)
|
|
15
|
+
: Node { token } { }
|
|
16
|
+
|
|
17
|
+
virtual Type type() const override { return Type::False; }
|
|
18
|
+
|
|
19
|
+
virtual void transform(Creator *creator) const override {
|
|
20
|
+
creator->set_type("false");
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
5
|
+
#include "tm/hashmap.hpp"
|
|
6
|
+
#include "tm/string.hpp"
|
|
7
|
+
|
|
8
|
+
namespace NatalieParser {
|
|
9
|
+
|
|
10
|
+
using namespace TM;
|
|
11
|
+
|
|
12
|
+
class FixnumNode : public Node {
|
|
13
|
+
public:
|
|
14
|
+
FixnumNode(const Token &token, long long number)
|
|
15
|
+
: Node { token }
|
|
16
|
+
, m_number { number } { }
|
|
17
|
+
|
|
18
|
+
virtual Type type() const override { return Type::Fixnum; }
|
|
19
|
+
|
|
20
|
+
virtual bool is_numeric() const override { return true; }
|
|
21
|
+
|
|
22
|
+
long long number() const { return m_number; }
|
|
23
|
+
|
|
24
|
+
virtual void transform(Creator *creator) const override {
|
|
25
|
+
creator->set_type("lit");
|
|
26
|
+
creator->append_integer(m_number);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
void negate() {
|
|
30
|
+
m_number *= -1;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected:
|
|
34
|
+
long long m_number;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
5
|
+
#include "tm/hashmap.hpp"
|
|
6
|
+
#include "tm/string.hpp"
|
|
7
|
+
|
|
8
|
+
namespace NatalieParser {
|
|
9
|
+
|
|
10
|
+
using namespace TM;
|
|
11
|
+
|
|
12
|
+
class FloatNode : public Node {
|
|
13
|
+
public:
|
|
14
|
+
FloatNode(const Token &token, double number)
|
|
15
|
+
: Node { token }
|
|
16
|
+
, m_number { number } { }
|
|
17
|
+
|
|
18
|
+
virtual Type type() const override { return Type::Float; }
|
|
19
|
+
|
|
20
|
+
virtual bool is_numeric() const override { return true; }
|
|
21
|
+
|
|
22
|
+
double number() const { return m_number; }
|
|
23
|
+
|
|
24
|
+
virtual void transform(Creator *creator) const override {
|
|
25
|
+
creator->set_type("lit");
|
|
26
|
+
creator->append_float(m_number);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
void negate() {
|
|
30
|
+
m_number *= -1;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected:
|
|
34
|
+
double m_number;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
5
|
+
#include "tm/hashmap.hpp"
|
|
6
|
+
#include "tm/string.hpp"
|
|
7
|
+
|
|
8
|
+
namespace NatalieParser {
|
|
9
|
+
|
|
10
|
+
using namespace TM;
|
|
11
|
+
|
|
12
|
+
class HashNode : public Node {
|
|
13
|
+
public:
|
|
14
|
+
HashNode(const Token &token)
|
|
15
|
+
: Node { token } { }
|
|
16
|
+
|
|
17
|
+
virtual Type type() const override { return Type::Hash; }
|
|
18
|
+
|
|
19
|
+
void add_node(SharedPtr<Node> node) {
|
|
20
|
+
m_nodes.push(node);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const Vector<SharedPtr<Node>> &nodes() const { return m_nodes; }
|
|
24
|
+
|
|
25
|
+
virtual void transform(Creator *creator) const override {
|
|
26
|
+
creator->set_type("hash");
|
|
27
|
+
for (auto node : m_nodes)
|
|
28
|
+
creator->append(node);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
protected:
|
|
32
|
+
Vector<SharedPtr<Node>> m_nodes {};
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/hash_node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node.hpp"
|
|
5
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
6
|
+
#include "tm/hashmap.hpp"
|
|
7
|
+
#include "tm/string.hpp"
|
|
8
|
+
|
|
9
|
+
namespace NatalieParser {
|
|
10
|
+
|
|
11
|
+
using namespace TM;
|
|
12
|
+
|
|
13
|
+
class HashPatternNode : public HashNode {
|
|
14
|
+
public:
|
|
15
|
+
HashPatternNode(const Token &token)
|
|
16
|
+
: HashNode { token } { }
|
|
17
|
+
|
|
18
|
+
virtual Type type() const override { return Type::HashPattern; }
|
|
19
|
+
|
|
20
|
+
virtual void transform(Creator *creator) const override {
|
|
21
|
+
creator->set_type("hash_pat");
|
|
22
|
+
creator->append_nil(); // NOTE: I don't know what this nil is for
|
|
23
|
+
for (auto node : m_nodes)
|
|
24
|
+
creator->append(node);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
5
|
+
#include "tm/hashmap.hpp"
|
|
6
|
+
#include "tm/string.hpp"
|
|
7
|
+
|
|
8
|
+
namespace NatalieParser {
|
|
9
|
+
|
|
10
|
+
using namespace TM;
|
|
11
|
+
|
|
12
|
+
class IdentifierNode : public Node {
|
|
13
|
+
public:
|
|
14
|
+
IdentifierNode(const Token &token, bool is_lvar)
|
|
15
|
+
: Node { token }
|
|
16
|
+
, m_is_lvar { is_lvar } { }
|
|
17
|
+
|
|
18
|
+
virtual Type type() const override { return Type::Identifier; }
|
|
19
|
+
|
|
20
|
+
virtual bool is_assignable() const override { return true; }
|
|
21
|
+
virtual bool can_accept_a_block() const override { return true; }
|
|
22
|
+
|
|
23
|
+
Token::Type token_type() const { return m_token.type(); }
|
|
24
|
+
|
|
25
|
+
SharedPtr<String> name() const { return m_token.literal_string(); }
|
|
26
|
+
|
|
27
|
+
void prepend_to_name(char c) {
|
|
28
|
+
auto literal = m_token.literal_string();
|
|
29
|
+
literal->prepend_char(c);
|
|
30
|
+
// FIXME: set_literal() unneeded?
|
|
31
|
+
m_token.set_literal(literal);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
void append_to_name(char c) {
|
|
35
|
+
auto literal = m_token.literal_string();
|
|
36
|
+
literal->append_char(c);
|
|
37
|
+
// FIXME: set_literal() unneeded?
|
|
38
|
+
m_token.set_literal(literal);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
virtual bool is_callable() const override {
|
|
42
|
+
switch (token_type()) {
|
|
43
|
+
case Token::Type::BareName:
|
|
44
|
+
case Token::Type::Constant:
|
|
45
|
+
return !m_is_lvar;
|
|
46
|
+
default:
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
bool is_lvar() const { return m_is_lvar; }
|
|
52
|
+
void set_is_lvar(bool is_lvar) { m_is_lvar = is_lvar; }
|
|
53
|
+
|
|
54
|
+
void add_to_locals(TM::Hashmap<TM::String> &locals) const {
|
|
55
|
+
if (token_type() == Token::Type::BareName)
|
|
56
|
+
locals.set(name()->c_str());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
virtual void transform(Creator *creator) const override {
|
|
60
|
+
if (creator->assignment())
|
|
61
|
+
return transform_assignment(creator);
|
|
62
|
+
switch (token_type()) {
|
|
63
|
+
case Token::Type::BareName:
|
|
64
|
+
if (is_lvar()) {
|
|
65
|
+
creator->set_type("lvar");
|
|
66
|
+
creator->append_symbol(name());
|
|
67
|
+
} else {
|
|
68
|
+
creator->set_type("call");
|
|
69
|
+
creator->append_nil();
|
|
70
|
+
creator->append_symbol(name());
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
case Token::Type::ClassVariable:
|
|
74
|
+
creator->set_type("cvar");
|
|
75
|
+
creator->append_symbol(name());
|
|
76
|
+
break;
|
|
77
|
+
case Token::Type::Constant:
|
|
78
|
+
creator->set_type("const");
|
|
79
|
+
creator->append_symbol(name());
|
|
80
|
+
break;
|
|
81
|
+
case Token::Type::GlobalVariable: {
|
|
82
|
+
creator->set_type("gvar");
|
|
83
|
+
creator->append_symbol(name());
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case Token::Type::InstanceVariable:
|
|
87
|
+
creator->set_type("ivar");
|
|
88
|
+
creator->append_symbol(name());
|
|
89
|
+
break;
|
|
90
|
+
default:
|
|
91
|
+
TM_UNREACHABLE();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
void transform_assignment(Creator *creator) const {
|
|
96
|
+
switch (token().type()) {
|
|
97
|
+
case Token::Type::BareName:
|
|
98
|
+
creator->set_type("lasgn");
|
|
99
|
+
break;
|
|
100
|
+
case Token::Type::ClassVariable:
|
|
101
|
+
creator->set_type("cvdecl");
|
|
102
|
+
break;
|
|
103
|
+
case Token::Type::Constant:
|
|
104
|
+
case Token::Type::ConstantResolution:
|
|
105
|
+
creator->set_type("cdecl");
|
|
106
|
+
break;
|
|
107
|
+
case Token::Type::GlobalVariable:
|
|
108
|
+
creator->set_type("gasgn");
|
|
109
|
+
break;
|
|
110
|
+
case Token::Type::InstanceVariable:
|
|
111
|
+
creator->set_type("iasgn");
|
|
112
|
+
break;
|
|
113
|
+
default:
|
|
114
|
+
printf("got token type %d\n", (int)token().type());
|
|
115
|
+
TM_UNREACHABLE();
|
|
116
|
+
}
|
|
117
|
+
creator->append_symbol(name());
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
protected:
|
|
121
|
+
bool m_is_lvar { false };
|
|
122
|
+
};
|
|
123
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
5
|
+
#include "tm/hashmap.hpp"
|
|
6
|
+
#include "tm/owned_ptr.hpp"
|
|
7
|
+
#include "tm/string.hpp"
|
|
8
|
+
|
|
9
|
+
namespace NatalieParser {
|
|
10
|
+
|
|
11
|
+
using namespace TM;
|
|
12
|
+
|
|
13
|
+
class IfNode : public Node {
|
|
14
|
+
public:
|
|
15
|
+
IfNode(const Token &token, SharedPtr<Node> condition, SharedPtr<Node> true_expr, SharedPtr<Node> false_expr)
|
|
16
|
+
: Node { token }
|
|
17
|
+
, m_condition { condition }
|
|
18
|
+
, m_true_expr { true_expr }
|
|
19
|
+
, m_false_expr { false_expr } {
|
|
20
|
+
assert(m_condition);
|
|
21
|
+
assert(m_true_expr);
|
|
22
|
+
assert(m_false_expr);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
virtual Type type() const override { return Type::If; }
|
|
26
|
+
|
|
27
|
+
const SharedPtr<Node> condition() const { return m_condition; }
|
|
28
|
+
const SharedPtr<Node> true_expr() const { return m_true_expr; }
|
|
29
|
+
const SharedPtr<Node> false_expr() const { return m_false_expr; }
|
|
30
|
+
|
|
31
|
+
virtual void transform(Creator *creator) const override {
|
|
32
|
+
creator->set_type("if");
|
|
33
|
+
creator->append(m_condition.ref());
|
|
34
|
+
creator->append(m_true_expr.ref());
|
|
35
|
+
creator->append(m_false_expr.ref());
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
protected:
|
|
39
|
+
SharedPtr<Node> m_condition {};
|
|
40
|
+
SharedPtr<Node> m_true_expr {};
|
|
41
|
+
SharedPtr<Node> m_false_expr {};
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "tm/owned_ptr.hpp"
|
|
5
|
+
#include "tm/string.hpp"
|
|
6
|
+
|
|
7
|
+
namespace NatalieParser {
|
|
8
|
+
|
|
9
|
+
using namespace TM;
|
|
10
|
+
|
|
11
|
+
class InfixOpNode : public Node {
|
|
12
|
+
public:
|
|
13
|
+
InfixOpNode(const Token &token, SharedPtr<Node> left, SharedPtr<String> op, SharedPtr<Node> right)
|
|
14
|
+
: Node { token }
|
|
15
|
+
, m_left { left }
|
|
16
|
+
, m_op { op }
|
|
17
|
+
, m_right { right } {
|
|
18
|
+
assert(m_left);
|
|
19
|
+
assert(m_op);
|
|
20
|
+
assert(m_right);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
virtual Type type() const override { return Type::InfixOp; }
|
|
24
|
+
|
|
25
|
+
virtual bool is_callable() const override { return true; }
|
|
26
|
+
virtual bool can_accept_a_block() const override { return false; }
|
|
27
|
+
|
|
28
|
+
const SharedPtr<Node> left() const { return m_left; }
|
|
29
|
+
const SharedPtr<String> op() const { return m_op; }
|
|
30
|
+
const SharedPtr<Node> right() const { return m_right; }
|
|
31
|
+
|
|
32
|
+
void set_right(SharedPtr<Node> right) { m_right = right; }
|
|
33
|
+
|
|
34
|
+
virtual void transform(Creator *creator) const override {
|
|
35
|
+
creator->set_type("call");
|
|
36
|
+
creator->append(m_left.ref());
|
|
37
|
+
creator->append_symbol(m_op);
|
|
38
|
+
creator->append(m_right.ref());
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
protected:
|
|
42
|
+
SharedPtr<Node> m_left {};
|
|
43
|
+
SharedPtr<String> m_op {};
|
|
44
|
+
SharedPtr<Node> m_right {};
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
5
|
+
#include "tm/hashmap.hpp"
|
|
6
|
+
#include "tm/string.hpp"
|
|
7
|
+
|
|
8
|
+
namespace NatalieParser {
|
|
9
|
+
|
|
10
|
+
using namespace TM;
|
|
11
|
+
|
|
12
|
+
class InterpolatedNode : public Node {
|
|
13
|
+
public:
|
|
14
|
+
InterpolatedNode(const Token &token)
|
|
15
|
+
: Node { token } { }
|
|
16
|
+
|
|
17
|
+
InterpolatedNode(const InterpolatedNode &other)
|
|
18
|
+
: Node { other.token() } {
|
|
19
|
+
for (auto node : other.nodes())
|
|
20
|
+
add_node(node);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
bool is_empty() const { return m_nodes.is_empty(); }
|
|
24
|
+
|
|
25
|
+
void prepend_node(SharedPtr<Node> node) { m_nodes.push_front(node); };
|
|
26
|
+
void add_node(SharedPtr<Node> node) { m_nodes.push(node); };
|
|
27
|
+
|
|
28
|
+
const Vector<SharedPtr<Node>> &nodes() const { return m_nodes; }
|
|
29
|
+
|
|
30
|
+
protected:
|
|
31
|
+
Vector<SharedPtr<Node>> m_nodes {};
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/interpolated_node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node.hpp"
|
|
5
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
6
|
+
#include "tm/hashmap.hpp"
|
|
7
|
+
#include "tm/string.hpp"
|
|
8
|
+
|
|
9
|
+
namespace NatalieParser {
|
|
10
|
+
|
|
11
|
+
using namespace TM;
|
|
12
|
+
|
|
13
|
+
class InterpolatedRegexpNode : public InterpolatedNode {
|
|
14
|
+
public:
|
|
15
|
+
InterpolatedRegexpNode(const Token &token)
|
|
16
|
+
: InterpolatedNode { token } { }
|
|
17
|
+
|
|
18
|
+
virtual Type type() const override { return Type::InterpolatedRegexp; }
|
|
19
|
+
|
|
20
|
+
int options() const { return m_options; }
|
|
21
|
+
void set_options(int options) { m_options = options; }
|
|
22
|
+
|
|
23
|
+
virtual void transform(Creator *creator) const override;
|
|
24
|
+
|
|
25
|
+
private:
|
|
26
|
+
int m_options { 0 };
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "natalie_parser/node/interpolated_node.hpp"
|
|
4
|
+
#include "natalie_parser/node/node.hpp"
|
|
5
|
+
#include "natalie_parser/node/node_with_args.hpp"
|
|
6
|
+
#include "tm/hashmap.hpp"
|
|
7
|
+
#include "tm/string.hpp"
|
|
8
|
+
|
|
9
|
+
namespace NatalieParser {
|
|
10
|
+
|
|
11
|
+
using namespace TM;
|
|
12
|
+
|
|
13
|
+
class InterpolatedShellNode : public InterpolatedNode {
|
|
14
|
+
public:
|
|
15
|
+
InterpolatedShellNode(const Token &token)
|
|
16
|
+
: InterpolatedNode { token } { }
|
|
17
|
+
|
|
18
|
+
virtual Type type() const override { return Type::InterpolatedShell; }
|
|
19
|
+
|
|
20
|
+
virtual void transform(Creator *creator) const override;
|
|
21
|
+
};
|
|
22
|
+
}
|