Skip to content

Commit

Permalink
more parser
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed May 28, 2022
1 parent a735503 commit ecaaef3
Show file tree
Hide file tree
Showing 12 changed files with 4,906 additions and 3,557 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

xcode:
cd xcode && cmake -D CMAKE_C_COMPILER="`xcrun -find cc`" -D CMAKE_CXX_COMPILER="`xcrun -find c++`" .. -GXcode
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ add_subdirectory(tests)

#add_library(typescript parser.cpp scanner.cpp utf.cpp syntax_cursor.cpp syntax_cursor.h ast.h parser2.h)
add_library(typescript scanner.cpp utf.cpp syntax_cursor.cpp syntax_cursor.h ast.h parser2.h types.cpp node_test.h path.h node_factory.h)
#add_library(typescript scanner.cpp utf.cpp syntax_cursor.cpp syntax_cursor.h ast.h)
#add_library(typescript scanner.cpp utf.cpp syntax_cursor.cpp syntax_cursor.h ast.h)
32 changes: 32 additions & 0 deletions src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ namespace ts {
return const_hash(s);
}

string substr(const string &str, int start, int length) {
if (start < 0) start += str.length();
if (length < 0) length = str.length() + length - start;
if (length < 0) return "";
return str.substr(start, length);
}

string substr(const string &str, int start) {
return substr(str, start, str.length());
}

/**
* shared_ptr has optional semantic already built-in, so we use it instead of std::optional,
* but instead of using shared_ptr directly, we use opt<T> to make it clear that it can be empty.
Expand Down Expand Up @@ -64,12 +75,22 @@ namespace ts {
return array.back();
}

template<typename T>
T defaultTo(optional<T> v, T def) {
if (v) return *v;
return def;
}

vector<string> charToStringVector(vector<const char *> chars) {
vector<string> s;
for (auto c: chars) s.push_back(c);
return s;
}

bool startsWith(const string &str, const string &suffix) {
return str.find(suffix) == 0;
}

bool endsWith(const string &str, const string &suffix) {
auto expectedPos = str.size() - suffix.size();
return expectedPos >= 0 && str.find(suffix, expectedPos) == expectedPos;
Expand Down Expand Up @@ -235,6 +256,17 @@ namespace ts {
return res;
}

template<typename K, typename V>
unordered_map<V, K> reverse(
const unordered_map<K, V> &map1
) {
unordered_map<V, K> res;
for (auto &i: map1) {
res[i.second] = i.first;
}
return res;
}

template<typename K, typename T>
static optional<T> get(unordered_map<K, T> &m, K key) {
auto v = m.find(key);
Expand Down
1 change: 1 addition & 0 deletions src/diagnostic_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ namespace ts {
static inline const auto String_literal_expected = diag(1141, DiagnosticCategory::Error, "String_literal_expected_1141", "String literal expected.");
static inline const auto Line_break_not_permitted_here = diag(1142, DiagnosticCategory::Error, "Line_break_not_permitted_here_1142", "Line break not permitted here.");
static inline const auto or_expected = diag(1144, DiagnosticCategory::Error, "or_expected_1144", "'{' or ';' expected.");
static inline const auto or_JSX_element_expected = diag(1145, DiagnosticCategory::Error, "or_JSX_element_expected_1145", "{' or JSX element expected.");
static inline const auto Declaration_expected = diag(1146, DiagnosticCategory::Error, "Declaration_expected_1146", "Declaration expected.");
static inline const auto Import_declarations_in_a_namespace_cannot_reference_a_module = diag(1147, DiagnosticCategory::Error, "Import_declarations_in_a_namespace_cannot_reference_a_module_1147", "Import declarations in a namespace cannot reference a module.");
static inline const auto Cannot_use_imports_exports_or_module_augmentations_when_module_is_none = diag(1148, DiagnosticCategory::Error, "Cannot_use_imports_exports_or_module_augmentations_when_module_is_none_1148", "Cannot use imports, exports, or module augmentations when '--module' is 'none'.");
Expand Down
2,635 changes: 1,619 additions & 1,016 deletions src/node_factory.h

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion src/node_test.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "types.h";
#include "types.h"

namespace ts {
// Literals
Expand All @@ -19,6 +19,23 @@ namespace ts {
throw runtime_error(format("resolveNamToNode with kind %d no valid name property", (int) node->kind));
}

shared<NodeUnion(JsxTagNameExpression)> getTagName(shared<NodeUnion(JsxOpeningElement, JsxOpeningFragment)> node) {
switch (node->kind) {
case SyntaxKind::JsxClosingElement: return node->to<JsxClosingElement>().tagName;
case SyntaxKind::JsxOpeningElement: return node->to<JsxOpeningElement>().tagName;
default: throw runtime_error(format("node %d has no tagName", node->kind));
}
}

string &getEscapedName(const shared<Node> &node) {
switch (node->kind) {
case SyntaxKind::Identifier: return dynamic_pointer_cast<Identifier>(node)->escapedText;
case SyntaxKind::PrivateIdentifier: return dynamic_pointer_cast<PrivateIdentifier>(node)->escapedText;
}

throw runtime_error(format("getEscapedName with kind %d no valid", (int) node->kind));
}

sharedOpt<NodeUnion(PropertyName)> getName(const shared<Node> &node) {
//[x] Check all with `name`
//[x] Check child of NamedDeclaration
Expand Down
Loading

0 comments on commit ecaaef3

Please sign in to comment.