Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ updates:
schedule:
interval: weekly
open-pull-requests-limit: 99
- package-ecosystem: cargo
directory: "/tree-sitter-mozcpp"
schedule:
interval: weekly
open-pull-requests-limit: 99
- package-ecosystem: cargo
directory: "/enums"
schedule:
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@
[submodule "tree-sitter-typescript"]
path = tree-sitter-typescript
url = https://github.com/tree-sitter/tree-sitter-typescript/
[submodule "tree-sitter-cpp"]
path = tree-sitter-cpp
url = https://github.com/tree-sitter/tree-sitter-cpp.git
31 changes: 26 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ fn main() {
"tree-sitter-ccomment".to_string(),
"tree-sitter-mozcpp".to_string(),
"tree-sitter-typescript".to_string(),
"tree-sitter-cpp".to_string(),
];
let dirs = collect_tree_sitter_dirs(ignore);
for dir in dirs {
Expand Down
9 changes: 5 additions & 4 deletions generate-moz-grammars/generate-mozcpp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
#
# Usage: ./generate-moz-grammars/generate-mozcpp.sh

# FIXME we need to remove this line once we are going to use
# the tree-sitter-cpp bindings
# Get the tree-sitter-cpp submodule version
TS_CPP_VERSION=`git submodule status tree-sitter-cpp | awk '{ print $1 }'`
# Set tree-sitter-cpp version
TS_CPP_VERSION="a35a275df92e7583df38f2de2562361f2b69987e"

# Enter the mozcpp directory
pushd tree-sitter-mozcpp
Expand All @@ -30,6 +28,9 @@ npm install -y
# Exit tree-sitter-cpp directory
popd

# Copy tree-sitter-cpp `scanner.cc` functions into the `src` directory
cp --verbose tree-sitter-cpp/src/scanner.cc ./src/scanner.cc

# Init npm
npm init -y

Expand Down
1 change: 0 additions & 1 deletion tree-sitter-cpp
Submodule tree-sitter-cpp deleted from a35a27
3 changes: 3 additions & 0 deletions tree-sitter-mozcpp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ tree-sitter = "^0.17"

[build-dependencies]
cc = "^1.0"
# This dependency is not used at all for this crate, but it is here so that
# dependabot can send notifications when there are updates for this grammar
tree-sitter-cpp = "0.19.0"
120 changes: 119 additions & 1 deletion tree-sitter-mozcpp/src/scanner.cc
Original file line number Diff line number Diff line change
@@ -1 +1,119 @@
#include "../../tree-sitter-cpp/src/scanner.cc"
#include <tree_sitter/parser.h>
#include <string>
#include <cwctype>

namespace {

using std::wstring;
using std::iswspace;

enum TokenType {
RAW_STRING_LITERAL,
};

struct Scanner {
bool scan(TSLexer *lexer, const bool *valid_symbols) {
while (iswspace(lexer->lookahead)) {
lexer->advance(lexer, true);
}

lexer->result_symbol = RAW_STRING_LITERAL;

// Raw string literals can start with: R, LR, uR, UR, u8R
// Consume 'R'
if (lexer->lookahead == 'L' || lexer->lookahead == 'U') {
lexer->advance(lexer, false);
if (lexer->lookahead != 'R') {
return false;
}
} else if (lexer->lookahead == 'u') {
lexer->advance(lexer, false);
if (lexer->lookahead == '8') {
lexer->advance(lexer, false);
if (lexer->lookahead != 'R') {
return false;
}
} else if (lexer->lookahead != 'R') {
return false;
}
} else if (lexer->lookahead != 'R') {
return false;
}
lexer->advance(lexer, false);

// Consume '"'
if (lexer->lookahead != '"') return false;
lexer->advance(lexer, false);

// Consume '(', delimiter
wstring delimiter;
for (;;) {
if (lexer->lookahead == 0 || lexer->lookahead == '\\' || iswspace(lexer->lookahead)) {
return false;
}
if (lexer->lookahead == '(') {
lexer->advance(lexer, false);
break;
}
delimiter += lexer->lookahead;
lexer->advance(lexer, false);
}

// Consume content, delimiter, ')', '"'
int delimiter_index = -1;
for (;;) {
if (lexer->lookahead == 0) return false;

if (delimiter_index >= 0) {
if (static_cast<unsigned>(delimiter_index) == delimiter.size()) {
if (lexer->lookahead == '"') {
lexer->advance(lexer, false);
return true;
} else {
delimiter_index = -1;
}
} else {
if (lexer->lookahead == delimiter[delimiter_index]) {
delimiter_index++;
} else {
delimiter_index = -1;
}
}
}

if (delimiter_index == -1 && lexer->lookahead == ')') {
delimiter_index = 0;
}

lexer->advance(lexer, false);
}
}
};

}

extern "C" {

void *tree_sitter_cpp_external_scanner_create() {
return new Scanner();
}

bool tree_sitter_cpp_external_scanner_scan(void *payload, TSLexer *lexer,
const bool *valid_symbols) {
Scanner *scanner = static_cast<Scanner *>(payload);
return scanner->scan(lexer, valid_symbols);
}

unsigned tree_sitter_cpp_external_scanner_serialize(void *payload, char *buffer) {
return 0;
}

void tree_sitter_cpp_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
}

void tree_sitter_cpp_external_scanner_destroy(void *payload) {
Scanner *scanner = static_cast<Scanner *>(payload);
delete scanner;
}

}