Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc #100

Merged
merged 5 commits into from
Sep 11, 2024
Merged

Misc #100

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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
*.pc

# Example dirs
/examples/playground/*.fs
/examples/playground/*.fsx
/examples/playground/

# Grammar volatiles
dsl.d.ts
Expand Down
50 changes: 31 additions & 19 deletions common/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
bool found_start_of_infix_op = false;
bool found_bracket_end = false;
bool found_preprocessor_end = false;
bool found_comment = false;
uint32_t indent_length = lexer->get_column(lexer);

for (;;) {
Expand All @@ -203,6 +204,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
} else if (lexer->lookahead == '/') {
skip(lexer);
if (!valid_symbols[INSIDE_STRING] && lexer->lookahead == '/') {
found_comment = true;
while (lexer->lookahead != '\n' && !lexer->eof(lexer)) {
skip(lexer);
}
Expand Down Expand Up @@ -234,7 +236,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
return true;
}
}
if (valid_symbols[PREPROC_END]) {
if (valid_symbols[PREPROC_END] && !found_comment) {
if (scanner->preprocessor_indents.size > 0) {
array_pop(&scanner->preprocessor_indents);
}
Expand Down Expand Up @@ -262,7 +264,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
return true;
}
}
if (valid_symbols[PREPROC_ELSE]) {
if (valid_symbols[PREPROC_ELSE] && !found_comment) {
lexer->mark_end(lexer);
lexer->result_symbol = PREPROC_ELSE;
return true;
Expand All @@ -280,12 +282,20 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
}
} else {
if (scanner->indents.size > 0) {
uint16_t current_indent_length = *array_back(&scanner->indents);
array_push(&scanner->preprocessor_indents, current_indent_length);
if (valid_symbols[PREPROC_IF]) {
uint16_t current_indent_length = *array_back(&scanner->indents);
array_push(&scanner->preprocessor_indents,
current_indent_length);
} else {
array_pop(&scanner->indents);
lexer->result_symbol = DEDENT;
return true;
}
} else if (!found_comment) {
lexer->mark_end(lexer);
lexer->result_symbol = PREPROC_IF;
return true;
}
lexer->mark_end(lexer);
lexer->result_symbol = PREPROC_IF;
return true;
}
}
} else {
Expand Down Expand Up @@ -411,7 +421,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
}
}
}
} else if (lexer->lookahead == 'a' && valid_symbols[AND]) {
} else if (lexer->lookahead == 'a' && valid_symbols[AND] && !found_comment) {
advance(lexer);
if (lexer->lookahead == 'n') {
advance(lexer);
Expand All @@ -426,7 +436,8 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
}
} else if (lexer->lookahead == 'e' &&
(valid_symbols[ELSE] || valid_symbols[ELIF] ||
valid_symbols[END] || valid_symbols[DEDENT])) {
valid_symbols[END] || valid_symbols[DEDENT]) &&
!found_comment) {
advance(lexer);
int16_t token_indent_level = lexer->get_column(lexer);
if (lexer->lookahead == 'l') {
Expand Down Expand Up @@ -501,16 +512,17 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
advance(lexer);
if (lexer->lookahead == 'd') {
advance(lexer);
if (valid_symbols[END]) {
lexer->mark_end(lexer);
lexer->result_symbol = END;
return true;
} else if (valid_symbols[DEDENT] && scanner->indents.size > 0) {
array_pop(&scanner->indents);
lexer->result_symbol = DEDENT;
return true;
} else {
return false;
if (lexer->lookahead == ' ' || lexer->lookahead == '\n' ||
lexer->eof(lexer)) {
if (valid_symbols[END]) {
lexer->mark_end(lexer);
lexer->result_symbol = END;
return true;
} else if (valid_symbols[DEDENT] && scanner->indents.size > 0) {
array_pop(&scanner->indents);
lexer->result_symbol = DEDENT;
return true;
}
}
}
}
Expand Down
48 changes: 18 additions & 30 deletions fsharp/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ module.exports = grammar({
$.type_check_pattern,
$.optional_pattern,
$.identifier_pattern,
$.named_field_pattern,
),

optional_pattern: ($) => prec.left(seq("?", $._pattern)),
Expand All @@ -331,7 +332,7 @@ module.exports = grammar({

attribute_pattern: ($) => prec.left(seq($.attributes, $._pattern)),

paren_pattern: ($) => seq("(", $._pattern, ")"),
paren_pattern: ($) => prec(1, seq("(", $._pattern, ")")),

as_pattern: ($) => prec.left(0, seq($._pattern, "as", $.identifier)),
cons_pattern: ($) => prec.left(0, seq($._pattern, "::", $._pattern)),
Expand Down Expand Up @@ -377,7 +378,7 @@ module.exports = grammar({
seq(
optional($._newline),
$._pattern,
repeat(seq(choice(";", $._newline), $._pattern)),
repeat(seq($._newline, $._pattern)),
),
$._indent,
$._dedent,
Expand All @@ -387,44 +388,31 @@ module.exports = grammar({
array_pattern: ($) => seq("[|", optional($._list_pattern_content), "|]"),
record_pattern: ($) =>
prec.left(
seq("{", $.field_pattern, repeat(seq(";", $.field_pattern)), "}"),
seq(
"{",
$.field_pattern,
repeat(seq($._newline, $.field_pattern)),
"}",
),
),

named_field: ($) => seq(optional(seq($.identifier, "=")), $._pattern),

named_field_pattern: ($) =>
prec.left(
seq("(", $.named_field, repeat(seq($._newline, $.named_field)), ")"),
),

identifier_pattern: ($) =>
prec.left(
-1,
seq(
$.long_identifier_or_op,
optional($._pattern_param),
optional($._pattern),
optional($._pattern),
),
),

_pattern_param: ($) =>
prec(
2,
choice(
$.const,
$.long_identifier,
// seq($.long_identifier, $._pattern_param),
// seq($._pattern_param, ":", $.type),
// seq(
// "[",
// $._pattern_param,
// repeat(seq($._seperator, $._pattern_param)),
// "]",
// ),
// seq(
// "(",
// $._pattern_param,
// repeat(seq($._seperator, $._pattern_param)),
// ")",
// ),
// seq("<@", $._expression, "@>"),
// seq("<@@", $._expression, "@@>"),
"null",
),
),
//
// Pattern rules (END)
//
Expand Down Expand Up @@ -1741,7 +1729,7 @@ module.exports = grammar({
seq(
"(",
/\s*/,
choice("?", /[!%&*+-./<=>@^|~][!%&*+-./<=>@^|~?]*/, ".. .."),
choice("?", /[!%&*+-./<=>@^|~$][!%&*+-./<=>@^|~?]*/, ".. .."),
/\s*/,
")",
),
Expand Down
Loading
Loading