Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

add unused_identifier and special_identifier #20

Merged
merged 3 commits into from
May 16, 2021
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
23 changes: 15 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ module.exports = grammar({
$.string_content,
$.string_end,
$.identifier,
$.unused_identifier,
$.special_identifier,
$.keyword_literal,
$.atom_literal,
$.atom_start,
Expand Down Expand Up @@ -166,6 +168,8 @@ module.exports = grammar({
[$.block, $.stab_expression]
],

inline: $ => [$._identifier],

word: $ => $.identifier,

rules: {
Expand Down Expand Up @@ -201,9 +205,12 @@ module.exports = grammar({
$.tuple,
$._literal,
$.char,
$.identifier
$._identifier
),

_identifier: $ =>
choice($.identifier, $.unused_identifier, $.special_identifier),

block: $ =>
seq(
"(",
Expand All @@ -224,7 +231,7 @@ module.exports = grammar({

paren_call: $ =>
seq(
field("function", alias($.identifier, $.function_identifier)),
field("function", alias($._identifier, $.function_identifier)),
$.arguments
),

Expand All @@ -236,7 +243,7 @@ module.exports = grammar({
field(
"function",
choice(
alias($.identifier, $.function_identifier),
alias($._identifier, $.function_identifier),
$.dot_call,
alias($.paren_call, $.call)
)
Expand All @@ -248,7 +255,7 @@ module.exports = grammar({
field(
"function",
choice(
alias($.identifier, $.function_identifier),
alias($._identifier, $.function_identifier),
$.dot_call,
alias($.paren_call, $.call)
)
Expand Down Expand Up @@ -358,7 +365,7 @@ module.exports = grammar({
choice(
...aliases(
[
$.identifier,
$._identifier,
$.true,
$.false,
$.nil,
Expand Down Expand Up @@ -395,7 +402,7 @@ module.exports = grammar({
"remote",
choice(
$.module,
alias($.identifier, $.remote_identifier),
$._identifier,
$.atom,
alias($._simple_dot_call, $.dot_call),
alias($.paren_call, $.call),
Expand Down Expand Up @@ -507,11 +514,11 @@ module.exports = grammar({
"%",
choice(
$.module,
$.identifier,
$._identifier,
$.atom,
alias($._simple_dot_call, $.dot_call),
alias($.paren_call, $.call),
seq("^", $.identifier)
seq("^", $._identifier)
),
"{",
optional($._terminator),
Expand Down
18 changes: 5 additions & 13 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
(atom_content)
(atom_end)] @tag

(comment) @comment
[(comment)
(unused_identifier)] @comment

(escape_sequence) @escape

Expand Down Expand Up @@ -50,8 +51,7 @@
left: (identifier) @variable.parameter
operator: _ @function
right: (identifier) @variable.parameter)]
(#match? @keyword "^(defp|def|defmacrop|defmacro|defguardp|defguard|defdelegate)$")
(#match? @variable.parameter "^[^_]"))
(#match? @keyword "^(defp|def|defmacrop|defmacro|defguardp|defguard|defdelegate)$"))

(call (function_identifier) @keyword
[(call
Expand All @@ -73,8 +73,7 @@
(_ (_ (identifier) @variable.parameter))
(_ (_ (_ (identifier) @variable.parameter)))
(_ (_ (_ (_ (identifier) @variable.parameter))))
(_ (_ (_ (_ (_ (identifier) @variable.parameter)))))]))
(#match? @variable.parameter "^[^_]"))
(_ (_ (_ (_ (_ (identifier) @variable.parameter)))))])))

(unary_op
operator: "@"
Expand Down Expand Up @@ -134,13 +133,6 @@
">>"
] @punctuation.bracket

[(identifier) @function.special
(#match? @function.special "^__.+__$")]

[(remote_identifier) @function.special
(#match? @function.special "^__.+__$")]

[(identifier) @comment
(#match? @comment "^_")]
(special_identifier) @function.special

(ERROR) @warning
32 changes: 29 additions & 3 deletions src/scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ enum TokenType {
STRING_END,

IDENTIFIER,
UNUSED_IDENTIFIER,
SPECIAL_IDENTIFIER,
KEYWORD_LITERAL,

ATOM_LITERAL,
Expand Down Expand Up @@ -146,6 +148,14 @@ struct Scanner {
return !is_identifier_body(c) && c != '?' && c != '!' && c != ':';
}

bool starts_with(std::string s, std::string needle) {
return s.rfind(needle, 0) == 0;
}

bool ends_with(std::string s, std::string needle) {
return s.length() >= needle.length() &&
(0 == s.compare(s.length() - needle.length(), needle.length(), needle));
}

int32_t sigil_terminator(int32_t c) {
switch (c) {
Expand Down Expand Up @@ -580,6 +590,16 @@ struct Scanner {
return false;
}

bool is_valid_identifier(TSLexer *lexer, const bool *valid_symbols, std::string token) {
if (starts_with(token, std::string("__")) && ends_with(token, std::string("__"))) {
return is_valid(lexer, valid_symbols, SPECIAL_IDENTIFIER, false);
}
if (starts_with(token, std::string("_"))) {
return is_valid(lexer, valid_symbols, UNUSED_IDENTIFIER, false);
}
return is_valid(lexer, valid_symbols, IDENTIFIER, false);
}

bool scan_identifier_or_keyword(TSLexer *lexer, const bool *valid_symbols) {
std::string token= "";

Expand Down Expand Up @@ -630,6 +650,7 @@ struct Scanner {
return false;
}

// ...
return is_valid(lexer, valid_symbols, IDENTIFIER, false);
}

Expand All @@ -642,6 +663,7 @@ struct Scanner {

if (lexer->lookahead == '?' ||
lexer->lookahead == '!') {
token.push_back(lexer->lookahead);
advance(lexer);
lexer->mark_end(lexer);

Expand All @@ -653,7 +675,7 @@ struct Scanner {
}
}

return is_identifier && is_valid(lexer, valid_symbols, IDENTIFIER, false);
return is_identifier && is_valid_identifier(lexer, valid_symbols, token);
} else if (lexer->lookahead == '@') {
is_identifier = false;
} else if (lexer->lookahead == ':') {
Expand All @@ -663,7 +685,7 @@ struct Scanner {
is_whitespace(lexer->lookahead)) {
return is_valid(lexer, valid_symbols, KEYWORD_LITERAL);
} else {
return is_identifier && is_valid(lexer, valid_symbols, IDENTIFIER, false);
return is_identifier && is_valid_identifier(lexer, valid_symbols, token);
}
} else if (!is_identifier_body(lexer->lookahead)) {
lexer->mark_end(lexer);
Expand Down Expand Up @@ -717,7 +739,7 @@ struct Scanner {
return is_valid(lexer, valid_symbols, ELSE);
}

return is_identifier && is_valid(lexer, valid_symbols, IDENTIFIER);
return is_identifier && is_valid_identifier(lexer, valid_symbols, token);
}
}
}
Expand Down Expand Up @@ -1340,6 +1362,8 @@ struct Scanner {
valid_symbols[SIGIL_START] ||
valid_symbols[KEYWORD_LITERAL] ||
valid_symbols[IDENTIFIER] ||
valid_symbols[UNUSED_IDENTIFIER] ||
valid_symbols[SPECIAL_IDENTIFIER] ||
valid_symbols[ATOM_LITERAL] ||
valid_symbols[ATOM_START] ||
valid_symbols[LINE_BREAK] ||
Expand Down Expand Up @@ -1386,6 +1410,8 @@ struct Scanner {
}

if ((valid_symbols[IDENTIFIER] ||
valid_symbols[UNUSED_IDENTIFIER] ||
valid_symbols[SPECIAL_IDENTIFIER] ||
valid_symbols[KEYWORD_LITERAL] ||
valid_symbols[TRUE] ||
valid_symbols[FALSE] ||
Expand Down
8 changes: 4 additions & 4 deletions test/corpus/call.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ a.()()
(program
(call
(dot_call
(remote_identifier)
(identifier)
(arguments))
(arguments)))

Expand Down Expand Up @@ -112,7 +112,7 @@ a.and

(program
(dot_call
(remote_identifier)
(identifier)
(function_identifier)))

================================================================================
Expand Down Expand Up @@ -157,7 +157,7 @@ end
(call
(function_identifier)
(arguments
(identifier)))
(special_identifier)))
(function_identifier)
(arguments
(anonymous_function
Expand Down Expand Up @@ -607,7 +607,7 @@ inspect(&__MODULE__."weirdly named/fun-"/0)
(unary_op
(binary_op
(dot_call
(remote_identifier)
(special_identifier)
(string
(string_start)
(string_content)
Expand Down
2 changes: 1 addition & 1 deletion test/corpus/case.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ end
(bare_arguments
(integer))
(dot_call
(remote_identifier)
(identifier)
(function_identifier)
(arguments))
(integer)))))
Expand Down
2 changes: 1 addition & 1 deletion test/corpus/cond.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ end
(bare_arguments
(integer))
(dot_call
(remote_identifier)
(identifier)
(function_identifier)
(arguments))
(integer)))))
Expand Down
2 changes: 1 addition & 1 deletion test/corpus/function.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ end
(identifier)))
(identifier)
(identifier)
(identifier)))))))
(special_identifier)))))))

================================================================================
multine def
Expand Down
4 changes: 2 additions & 2 deletions test/corpus/struct.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ identifier

(program
(struct
(identifier)))
(unused_identifier)))

================================================================================
with fields
Expand Down Expand Up @@ -55,5 +55,5 @@ with dot call
(program
(struct
(dot_call
(remote_identifier)
(special_identifier)
(module))))
2 changes: 1 addition & 1 deletion test/corpus/try.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ end
(atom_literal)))
(stab_expression
(bare_arguments
(identifier))
(unused_identifier))
(atom
(atom_literal)))))))

Expand Down
41 changes: 41 additions & 0 deletions test/highlight/sandbox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,25 @@ defmodule Long.Module.Name do
# ^ tag
# ^ tag

def func(_foo?, _bar!, <<_baz::binary>>), do: :ok
# ^ keyword
# ^ function
# ^ punctuation.bracket
# ^ comment
# ^ comment
# ^ punctuation.delimiter
# ^ comment
# ^ punctuation.delimiter
# ^ punctuation.bracket
# ^ comment
# ^ operator
# ^ variable.parameter
# ^ punctuation.bracket
# ^ punctuation.bracket
# ^ punctuation.delimiter
# ^ tag
# ^ tag

# Function
def f(x), do: x
# ^ keyword
Expand Down Expand Up @@ -1088,6 +1107,28 @@ defprotocol Useless do
# ^ punctuation.bracket

def func3(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
# ^ keyword
# ^ function
# ^ punctuation.bracket
# ^ variable.parameter
# ^ punctuation.delimiter
# ^ variable.parameter
# ^ punctuation.delimiter
# ^ variable.parameter
# ^ punctuation.delimiter
# ^ variable.parameter
# ^ punctuation.delimiter
# ^ variable.parameter
# ^ punctuation.delimiter
# ^ variable.parameter
# ^ punctuation.delimiter
# ^ variable.parameter
# ^ punctuation.delimiter
# ^ variable.parameter
# ^ punctuation.delimiter
# ^ variable.parameter
# ^ punctuation.bracket

end

defimpl Useless, for: Atom do
Expand Down