elm-tooling / tree-sitter-elm

Tree sitter implementation for elm
https://elm-tooling.github.io/tree-sitter-elm/
MIT License
75 stars 14 forks source link

Named anonymous function #11

Closed buinauskas closed 5 years ago

buinauskas commented 5 years ago

I've found this valid code in one of Elm repositories which fails parsing (source):

makePows start base =
    if start < 0 then
        \x -> -(base ^ -x)

    else
        \x -> base ^ x

The following has to be added to parse-examples:

checkout_at "examples/elm-visualization" "gampleman/elm-visualization" "b666361498464845b2b9d3da4ff2661cb7f4ae4b"

I've attempted to write a test case for that, however I always ended up with a failure and not so much clue how I should continue with it. That's the test I've written:

=====================================
Named anonymous function
=====================================

makePows start base =
    if start < 0 then
        \x -> -(base ^ -x)

    else
        \x -> base ^ x

---
(file
        (value_declaration
                (function_declaration_left (lower_case_identifier) (lower_pattern (lower_case_identifier)) (lower_pattern (lower_case_identifier)))
                (eq)
                (if_else_expr
                        (if)
                        (bin_op_expr
                                (value_qid (lower_case_identifier))
                                (operator (operator_identifier))
                                (number_literal)
                        )
                        (then)
                        (anonymous_function_expr
                                (backslash)
                                (pattern (lower_pattern (lower_case_identifier)))
                                (arrow)
                                (negate_expr
                                        (parenthesized_expr
                                                (left_parenthesis)
                                                (bin_op_expr)
                                                (value_expr (value_qid (lower_case_identifier)))
                                                (bin_op_expr)
                                                (negate_expr
                                                        (operator_identifier)
                                                        (value_expr (value_qid (lower_case_identifier)))
                                                )
                                                (right_parenthesis)
                                        )
                                )
                        )
                        (else)
                        (anonymous_function_expr
                                (backslash)
                                (pattern (lower_pattern (lower_case_identifier)))
                                (arrow)
                                (value_expr (value_qid (lower_case_identifier)))
                                (operator (operator_identifier))
                                (value_expr (value_qid (lower_case_identifier)))
                        )
                )
        )
)

If you'd be able to point out what I did wrong - I'd appreciate it.

razzeee commented 5 years ago

Another scanner.cc error, replace if (lookahead >= 'a' && lookahead <= 'z') with something like if (lookahead >= 'a' && lookahead <= 'z' || lookahead == '(')

This works, but my C++ feels rusty. :)

buinauskas commented 5 years ago

All right. Will try that! 👍