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

Fail to parse expression #173

Open
mingodad opened this issue Feb 11, 2024 · 4 comments
Open

Fail to parse expression #173

mingodad opened this issue Feb 11, 2024 · 4 comments

Comments

@mingodad
Copy link

Problem description

Testing a fresh build of this repository I found that it fails to parse a piece of code that the current online playground parse fine (as pointed out elsewhere the online playground is outdated).

public void character(int c, String name, String category, int upper, int lower, int title) {
	assertTrue(c > prevsym);
	prevsym++;
	while (prevsym < c) {
		yield(prevsym++, "cn");
	}
	yield(c, category);

}

Expected parse tree output (optional)

Online playground:

[translation_unit](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [6, 0]
  [function_definition](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [5, 1]
    type: [type_identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 0] - [0, 6]
    [ERROR](https://tree-sitter.github.io/tree-sitter/playground#) [0, 7] - [0, 11]
      [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 7] - [0, 11]
    declarator: [function_declarator](https://tree-sitter.github.io/tree-sitter/playground#) [0, 12] - [0, 41]
      declarator: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 12] - [0, 21]
      parameters: [parameter_list](https://tree-sitter.github.io/tree-sitter/playground#) [0, 21] - [0, 41]
        [parameter_declaration](https://tree-sitter.github.io/tree-sitter/playground#) [0, 22] - [0, 27]
          type: [primitive_type](https://tree-sitter.github.io/tree-sitter/playground#) [0, 22] - [0, 25]
          declarator: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 26] - [0, 27]
        [parameter_declaration](https://tree-sitter.github.io/tree-sitter/playground#) [0, 29] - [0, 40]
          type: [primitive_type](https://tree-sitter.github.io/tree-sitter/playground#) [0, 29] - [0, 32]
          declarator: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [0, 33] - [0, 40]
    body: [compound_statement](https://tree-sitter.github.io/tree-sitter/playground#) [0, 42] - [5, 1]
      [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [1, 1] - [1, 11]
        [update_expression](https://tree-sitter.github.io/tree-sitter/playground#) [1, 1] - [1, 10]
          argument: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [1, 1] - [1, 8]
      [while_statement](https://tree-sitter.github.io/tree-sitter/playground#) [2, 1] - [4, 2]
        condition: [parenthesized_expression](https://tree-sitter.github.io/tree-sitter/playground#) [2, 7] - [2, 20]
          [binary_expression](https://tree-sitter.github.io/tree-sitter/playground#) [2, 8] - [2, 19]
            left: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [2, 8] - [2, 15]
            right: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [2, 18] - [2, 19]
        body: [compound_statement](https://tree-sitter.github.io/tree-sitter/playground#) [2, 21] - [4, 2]
          [expression_statement](https://tree-sitter.github.io/tree-sitter/playground#) [3, 2] - [3, 25]
            [call_expression](https://tree-sitter.github.io/tree-sitter/playground#) [3, 2] - [3, 24]
              function: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [3, 2] - [3, 7]
              arguments: [argument_list](https://tree-sitter.github.io/tree-sitter/playground#) [3, 7] - [3, 24]
                [update_expression](https://tree-sitter.github.io/tree-sitter/playground#) [3, 8] - [3, 17]
                  argument: [identifier](https://tree-sitter.github.io/tree-sitter/playground#) [3, 8] - [3, 15]
                [string_literal](https://tree-sitter.github.io/tree-sitter/playground#) [3, 19] - [3, 23]

Actual parse tree output (optional)

Local fresh build playground:

[program](http://127.0.0.1:8001/#) [0, 0] - [6, 0]
  [method_declaration](http://127.0.0.1:8001/#) [0, 0] - [5, 1]
    [modifiers](http://127.0.0.1:8001/#) [0, 0] - [0, 6]
    type: [void_type](http://127.0.0.1:8001/#) [0, 7] - [0, 11]
    name: [identifier](http://127.0.0.1:8001/#) [0, 12] - [0, 21]
    parameters: [formal_parameters](http://127.0.0.1:8001/#) [0, 21] - [0, 41]
      [formal_parameter](http://127.0.0.1:8001/#) [0, 22] - [0, 27]
        type: [integral_type](http://127.0.0.1:8001/#) [0, 22] - [0, 25]
        name: [identifier](http://127.0.0.1:8001/#) [0, 26] - [0, 27]
      [formal_parameter](http://127.0.0.1:8001/#) [0, 29] - [0, 40]
        type: [integral_type](http://127.0.0.1:8001/#) [0, 29] - [0, 32]
        name: [identifier](http://127.0.0.1:8001/#) [0, 33] - [0, 40]
    body: [block](http://127.0.0.1:8001/#) [0, 42] - [5, 1]
      [expression_statement](http://127.0.0.1:8001/#) [1, 1] - [1, 11]
        [update_expression](http://127.0.0.1:8001/#) [1, 1] - [1, 10]
          [identifier](http://127.0.0.1:8001/#) [1, 1] - [1, 8]
      [while_statement](http://127.0.0.1:8001/#) [2, 1] - [4, 2]
        condition: [condition](http://127.0.0.1:8001/#) [2, 7] - [2, 20]
          [binary_expression](http://127.0.0.1:8001/#) [2, 8] - [2, 19]
            left: [identifier](http://127.0.0.1:8001/#) [2, 8] - [2, 15]
            right: [identifier](http://127.0.0.1:8001/#) [2, 18] - [2, 19]
        body: [block](http://127.0.0.1:8001/#) [2, 21] - [4, 2]
          [yield_statement](http://127.0.0.1:8001/#) [3, 2] - [3, 25]
            [parenthesized_expression](http://127.0.0.1:8001/#) [3, 7] - [3, 24]
              [ERROR](http://127.0.0.1:8001/#) [3, 8] - [3, 18]
                [update_expression](http://127.0.0.1:8001/#) [3, 8] - [3, 17]
                  [identifier](http://127.0.0.1:8001/#) [3, 8] - [3, 15]
              [string_literal](http://127.0.0.1:8001/#) [3, 19] - [3, 23]
                [string_fragment](http://127.0.0.1:8001/#) [3, 20] - [3, 22]
@mingodad
Copy link
Author

Also fail to parse the code shown bellow, it seems that module is been recognized as keyword but shouldn't in the annotation context.

public class WasmSupport {

    @Import(name = "putwcharsOut", module = "teavm")
    public staticvoid putCharsStderr();

    @Import(module = "teavmMath", name = "random")
    public static double random();

}

Output:

[program](http://127.0.0.1:8001/#) [0, 0] - [10, 0]
  [class_declaration](http://127.0.0.1:8001/#) [0, 0] - [8, 1]
    [modifiers](http://127.0.0.1:8001/#) [0, 0] - [0, 6]
    name: [identifier](http://127.0.0.1:8001/#) [0, 13] - [0, 24]
    body: [class_body](http://127.0.0.1:8001/#) [0, 25] - [8, 1]
      [method_declaration](http://127.0.0.1:8001/#) [2, 4] - [3, 39]
        [modifiers](http://127.0.0.1:8001/#) [2, 4] - [3, 10]
          [annotation](http://127.0.0.1:8001/#) [2, 4] - [2, 52]
            name: [identifier](http://127.0.0.1:8001/#) [2, 5] - [2, 11]
            arguments: [annotation_argument_list](http://127.0.0.1:8001/#) [2, 11] - [2, 52]
              [element_value_pair](http://127.0.0.1:8001/#) [2, 12] - [2, 33]
                key: [identifier](http://127.0.0.1:8001/#) [2, 12] - [2, 16]
                value: [string_literal](http://127.0.0.1:8001/#) [2, 19] - [2, 33]
                  [string_fragment](http://127.0.0.1:8001/#) [2, 20] - [2, 32]
              [element_value_pair](http://127.0.0.1:8001/#) [2, 35] - [2, 51]
                key: [identifier](http://127.0.0.1:8001/#) [2, 35] - [2, 41]
                value: [string_literal](http://127.0.0.1:8001/#) [2, 44] - [2, 51]
                  [string_fragment](http://127.0.0.1:8001/#) [2, 45] - [2, 50]
        type: [type_identifier](http://127.0.0.1:8001/#) [3, 11] - [3, 21]
        name: [identifier](http://127.0.0.1:8001/#) [3, 22] - [3, 36]
        parameters: [formal_parameters](http://127.0.0.1:8001/#) [3, 36] - [3, 38]
      [method_declaration](http://127.0.0.1:8001/#) [5, 4] - [6, 34]
        [modifiers](http://127.0.0.1:8001/#) [5, 4] - [6, 17]
          [annotation](http://127.0.0.1:8001/#) [5, 4] - [5, 50]
            name: [identifier](http://127.0.0.1:8001/#) [5, 5] - [5, 11]
            arguments: [annotation_argument_list](http://127.0.0.1:8001/#) [5, 11] - [5, 50]
              [ERROR](http://127.0.0.1:8001/#) [5, 12] - [5, 33]
                [assignment_expression](http://127.0.0.1:8001/#) [5, 12] - [5, 32]
                  left: [identifier](http://127.0.0.1:8001/#) [5, 12] - [5, 18]
                  right: [string_literal](http://127.0.0.1:8001/#) [5, 21] - [5, 32]
                    [string_fragment](http://127.0.0.1:8001/#) [5, 22] - [5, 31]
              [element_value_pair](http://127.0.0.1:8001/#) [5, 34] - [5, 49]
                key: [identifier](http://127.0.0.1:8001/#) [5, 34] - [5, 38]
                value: [string_literal](http://127.0.0.1:8001/#) [5, 41] - [5, 49]
                  [string_fragment](http://127.0.0.1:8001/#) [5, 42] - [5, 48]
        type: [floating_point_type](http://127.0.0.1:8001/#) [6, 18] - [6, 24]
        name: [identifier](http://127.0.0.1:8001/#) [6, 25] - [6, 31]
        parameters: [formal_parameters](http://127.0.0.1:8001/#) [6, 31] - [6, 33]

@mingodad
Copy link
Author

It also fail to parse hex numbers:

private char getNoNumberChar(){
	char ch = 0x7e-0x21;
	return ch;
}

Output:

[program](http://127.0.0.1:8001/#) [0, 0] - [4, 0]
  [method_declaration](http://127.0.0.1:8001/#) [0, 0] - [3, 1]
    [modifiers](http://127.0.0.1:8001/#) [0, 0] - [0, 7]
    type: [integral_type](http://127.0.0.1:8001/#) [0, 8] - [0, 12]
    name: [identifier](http://127.0.0.1:8001/#) [0, 13] - [0, 28]
    parameters: [formal_parameters](http://127.0.0.1:8001/#) [0, 28] - [0, 30]
    body: [block](http://127.0.0.1:8001/#) [0, 30] - [3, 1]
      [local_variable_declaration](http://127.0.0.1:8001/#) [1, 1] - [1, 21]
        type: [integral_type](http://127.0.0.1:8001/#) [1, 1] - [1, 5]
        declarator: [variable_declarator](http://127.0.0.1:8001/#) [1, 6] - [1, 17]
          name: [identifier](http://127.0.0.1:8001/#) [1, 6] - [1, 8]
          value: [hex_floating_point_literal](http://127.0.0.1:8001/#) [1, 11] - [1, 17]
        [ERROR](http://127.0.0.1:8001/#) [1, 17] - [1, 20]
          [identifier](http://127.0.0.1:8001/#) [1, 17] - [1, 20]
      [return_statement](http://127.0.0.1:8001/#) [2, 1] - [2, 11]
        [identifier](http://127.0.0.1:8001/#) [2, 8] - [2, 10]

@mingodad
Copy link
Author

It seems that the incorrect parsing as hex_floating_point_literal is a copy and paste problem and the diff shown bellow seems to fix it.

---------------------------------- grammar.js ----------------------------------
index bfda731..eead70f 100644
@@ -125,7 +125,7 @@ module.exports = grammar({
     decimal_floating_point_literal: $ => token(choice(
       seq(DECIMAL_DIGITS, '.', optional(DECIMAL_DIGITS), optional(seq((/[eE]/), optional(choice('-', '+')), DECIMAL_DIGITS)), optional(/[fFdD]/)),
       seq('.', DECIMAL_DIGITS, optional(seq((/[eE]/), optional(choice('-', '+')), DECIMAL_DIGITS)), optional(/[fFdD]/)),
-      seq(DIGITS, /[eEpP]/, optional(choice('-', '+')), DECIMAL_DIGITS, optional(/[fFdD]/)),
+      seq(DIGITS, /[eE]/, optional(choice('-', '+')), DECIMAL_DIGITS, optional(/[fFdD]/)),
       seq(DIGITS, optional(seq((/[eE]/), optional(choice('-', '+')), DECIMAL_DIGITS)), (/[fFdD]/))
     )),
 
@@ -136,7 +136,7 @@ module.exports = grammar({
         seq(optional(HEX_DIGITS), '.', HEX_DIGITS)
       ),
       optional(seq(
-        /[eEpP]/,
+        /[pP]/,
         optional(choice('-', '+')),
         DIGITS,
         optional(/[fFdD]/)

@mingodad
Copy link
Author

The same incorrect parsing as hex_floating_point_literal error is present int tree-sitter-c (and maybe others via copy and paste).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant