From 9e0e5c0e0b35eea793af5639a377f5d899b971f3 Mon Sep 17 00:00:00 2001 From: Jon Sully Date: Sun, 30 Jul 2023 21:06:51 +0100 Subject: [PATCH] LSP: Add Go To Definition support for `Ast::ModuleAccess` --- .../module_access_component_constant | 83 ++++++++++++++++++ .../module_access_component_function | 81 ++++++++++++++++++ .../module_access_component_gets | 85 +++++++++++++++++++ .../module_access_component_name | 81 ++++++++++++++++++ .../module_access_component_state | 83 ++++++++++++++++++ .../module_access_module_constant | 79 +++++++++++++++++ .../module_access_module_function | 81 ++++++++++++++++++ .../location_link/module_access_module_name | 81 ++++++++++++++++++ .../module_access_store_constant | 79 +++++++++++++++++ .../module_access_store_function | 81 ++++++++++++++++++ .../location_link/module_access_store_gets | 81 ++++++++++++++++++ .../location_link/module_access_store_name | 81 ++++++++++++++++++ .../location_link/module_access_store_state | 79 +++++++++++++++++ src/ls/definition/module_access.cr | 20 +++++ src/ls/definition/type_id.cr | 3 +- src/parsers/connect_variable.cr | 2 +- src/parsers/constant_access.cr | 4 +- src/parsers/constant_variable.cr | 24 ------ src/parsers/variable.cr | 8 +- 19 files changed, 1086 insertions(+), 30 deletions(-) create mode 100644 spec/language_server/definition/location_link/module_access_component_constant create mode 100644 spec/language_server/definition/location_link/module_access_component_function create mode 100644 spec/language_server/definition/location_link/module_access_component_gets create mode 100644 spec/language_server/definition/location_link/module_access_component_name create mode 100644 spec/language_server/definition/location_link/module_access_component_state create mode 100644 spec/language_server/definition/location_link/module_access_module_constant create mode 100644 spec/language_server/definition/location_link/module_access_module_function create mode 100644 spec/language_server/definition/location_link/module_access_module_name create mode 100644 spec/language_server/definition/location_link/module_access_store_constant create mode 100644 spec/language_server/definition/location_link/module_access_store_function create mode 100644 spec/language_server/definition/location_link/module_access_store_gets create mode 100644 spec/language_server/definition/location_link/module_access_store_name create mode 100644 spec/language_server/definition/location_link/module_access_store_state create mode 100644 src/ls/definition/module_access.cr delete mode 100644 src/parsers/constant_variable.cr diff --git a/spec/language_server/definition/location_link/module_access_component_constant b/spec/language_server/definition/location_link/module_access_component_constant new file mode 100644 index 000000000..01d5a2dda --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_component_constant @@ -0,0 +1,83 @@ +global component Header { + const TITLE = "Mint" + + fun render : Html { +
+ } +} +----------------------------------------------------------------file header.mint +component Test { + fun render : Html { +
+ <{ Header:TITLE }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 16 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 16 + }, + "end": { + "line": 3, + "character": 21 + } + }, + "targetUri": "file://#{root_path}/header.mint", + "targetRange": { + "start": { + "line": 1, + "character": 2 + }, + "end": { + "line": 1, + "character": 22 + } + }, + "targetSelectionRange": { + "start": { + "line": 1, + "character": 8 + }, + "end": { + "line": 1, + "character": 13 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_component_function b/spec/language_server/definition/location_link/module_access_component_function new file mode 100644 index 000000000..0b029b2aa --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_component_function @@ -0,0 +1,81 @@ +global component Header { + fun render : Html { +
+ } +} +----------------------------------------------------------------file header.mint +component Test { + fun render : Html { +
+ <{ Header.render() }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 16 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 16 + }, + "end": { + "line": 3, + "character": 22 + } + }, + "targetUri": "file://#{root_path}/header.mint", + "targetRange": { + "start": { + "line": 1, + "character": 2 + }, + "end": { + "line": 3, + "character": 3 + } + }, + "targetSelectionRange": { + "start": { + "line": 1, + "character": 6 + }, + "end": { + "line": 1, + "character": 12 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_component_gets b/spec/language_server/definition/location_link/module_access_component_gets new file mode 100644 index 000000000..2555b6ee1 --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_component_gets @@ -0,0 +1,85 @@ +global component Header { + get title : String { + "Mint" + } + + fun render : Html { +
+ } +} +----------------------------------------------------------------file header.mint +component Test { + fun render : Html { +
+ <{ Header.title }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 16 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 16 + }, + "end": { + "line": 3, + "character": 21 + } + }, + "targetUri": "file://#{root_path}/header.mint", + "targetRange": { + "start": { + "line": 1, + "character": 2 + }, + "end": { + "line": 5, + "character": 2 + } + }, + "targetSelectionRange": { + "start": { + "line": 1, + "character": 6 + }, + "end": { + "line": 1, + "character": 11 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_component_name b/spec/language_server/definition/location_link/module_access_component_name new file mode 100644 index 000000000..b669f0b49 --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_component_name @@ -0,0 +1,81 @@ +global component Header { + fun render : Html { +
+ } +} +----------------------------------------------------------------file header.mint +component Test { + fun render : Html { +
+ <{ Header.render() }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 9 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 9 + }, + "end": { + "line": 3, + "character": 15 + } + }, + "targetUri": "file://#{root_path}/header.mint", + "targetRange": { + "start": { + "line": 0, + "character": 0 + }, + "end": { + "line": 4, + "character": 1 + } + }, + "targetSelectionRange": { + "start": { + "line": 0, + "character": 17 + }, + "end": { + "line": 0, + "character": 23 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_component_state b/spec/language_server/definition/location_link/module_access_component_state new file mode 100644 index 000000000..1208ef692 --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_component_state @@ -0,0 +1,83 @@ +global component Header { + state title = "Mint" + + fun render : Html { +
+ } +} +----------------------------------------------------------------file header.mint +component Test { + fun render : Html { +
+ <{ Header.title }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 16 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 16 + }, + "end": { + "line": 3, + "character": 21 + } + }, + "targetUri": "file://#{root_path}/header.mint", + "targetRange": { + "start": { + "line": 1, + "character": 2 + }, + "end": { + "line": 1, + "character": 22 + } + }, + "targetSelectionRange": { + "start": { + "line": 1, + "character": 8 + }, + "end": { + "line": 1, + "character": 13 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_module_constant b/spec/language_server/definition/location_link/module_access_module_constant new file mode 100644 index 000000000..6896b0322 --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_module_constant @@ -0,0 +1,79 @@ +module Header { + const TITLE = "Mint" +} +----------------------------------------------------------------file module.mint +component Test { + fun render : Html { +
+ <{ Header:TITLE }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 16 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 16 + }, + "end": { + "line": 3, + "character": 21 + } + }, + "targetUri": "file://#{root_path}/module.mint", + "targetRange": { + "start": { + "line": 1, + "character": 2 + }, + "end": { + "line": 1, + "character": 22 + } + }, + "targetSelectionRange": { + "start": { + "line": 1, + "character": 8 + }, + "end": { + "line": 1, + "character": 13 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_module_function b/spec/language_server/definition/location_link/module_access_module_function new file mode 100644 index 000000000..5488f5465 --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_module_function @@ -0,0 +1,81 @@ +module Header { + fun title : String { + "Mint" + } +} +----------------------------------------------------------------file module.mint +component Test { + fun render : Html { +
+ <{ Header.title() }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 16 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 16 + }, + "end": { + "line": 3, + "character": 21 + } + }, + "targetUri": "file://#{root_path}/module.mint", + "targetRange": { + "start": { + "line": 1, + "character": 2 + }, + "end": { + "line": 3, + "character": 3 + } + }, + "targetSelectionRange": { + "start": { + "line": 1, + "character": 6 + }, + "end": { + "line": 1, + "character": 11 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_module_name b/spec/language_server/definition/location_link/module_access_module_name new file mode 100644 index 000000000..8b86b1571 --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_module_name @@ -0,0 +1,81 @@ +module Header { + fun title : String { + "Mint" + } +} +----------------------------------------------------------------file module.mint +component Test { + fun render : Html { +
+ <{ Header.title() }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 9 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 9 + }, + "end": { + "line": 3, + "character": 15 + } + }, + "targetUri": "file://#{root_path}/module.mint", + "targetRange": { + "start": { + "line": 0, + "character": 0 + }, + "end": { + "line": 4, + "character": 1 + } + }, + "targetSelectionRange": { + "start": { + "line": 0, + "character": 7 + }, + "end": { + "line": 0, + "character": 13 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_store_constant b/spec/language_server/definition/location_link/module_access_store_constant new file mode 100644 index 000000000..4fbb6fb7b --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_store_constant @@ -0,0 +1,79 @@ +store Header { + const TITLE = "Mint" +} +-----------------------------------------------------------------file store.mint +component Test { + fun render : Html { +
+ <{ Header:TITLE }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 16 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 16 + }, + "end": { + "line": 3, + "character": 21 + } + }, + "targetUri": "file://#{root_path}/store.mint", + "targetRange": { + "start": { + "line": 1, + "character": 2 + }, + "end": { + "line": 1, + "character": 22 + } + }, + "targetSelectionRange": { + "start": { + "line": 1, + "character": 8 + }, + "end": { + "line": 1, + "character": 13 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_store_function b/spec/language_server/definition/location_link/module_access_store_function new file mode 100644 index 000000000..169011196 --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_store_function @@ -0,0 +1,81 @@ +store Header { + fun title : String { + "Mint" + } +} +-----------------------------------------------------------------file store.mint +component Test { + fun render : Html { +
+ <{ Header.title() }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 16 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 16 + }, + "end": { + "line": 3, + "character": 21 + } + }, + "targetUri": "file://#{root_path}/store.mint", + "targetRange": { + "start": { + "line": 1, + "character": 2 + }, + "end": { + "line": 3, + "character": 3 + } + }, + "targetSelectionRange": { + "start": { + "line": 1, + "character": 6 + }, + "end": { + "line": 1, + "character": 11 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_store_gets b/spec/language_server/definition/location_link/module_access_store_gets new file mode 100644 index 000000000..15b5e99be --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_store_gets @@ -0,0 +1,81 @@ +store Header { + get title : String { + "Mint" + } +} +-----------------------------------------------------------------file store.mint +component Test { + fun render : Html { +
+ <{ Header.title }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 16 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 16 + }, + "end": { + "line": 3, + "character": 21 + } + }, + "targetUri": "file://#{root_path}/store.mint", + "targetRange": { + "start": { + "line": 1, + "character": 2 + }, + "end": { + "line": 4, + "character": 0 + } + }, + "targetSelectionRange": { + "start": { + "line": 1, + "character": 6 + }, + "end": { + "line": 1, + "character": 11 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_store_name b/spec/language_server/definition/location_link/module_access_store_name new file mode 100644 index 000000000..96c9eb66e --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_store_name @@ -0,0 +1,81 @@ +store Header { + fun title : String { + "Mint" + } +} +-----------------------------------------------------------------file store.mint +component Test { + fun render : Html { +
+ <{ Header.title() }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 9 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 9 + }, + "end": { + "line": 3, + "character": 15 + } + }, + "targetUri": "file://#{root_path}/store.mint", + "targetRange": { + "start": { + "line": 0, + "character": 0 + }, + "end": { + "line": 4, + "character": 1 + } + }, + "targetSelectionRange": { + "start": { + "line": 0, + "character": 6 + }, + "end": { + "line": 0, + "character": 12 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/spec/language_server/definition/location_link/module_access_store_state b/spec/language_server/definition/location_link/module_access_store_state new file mode 100644 index 000000000..b473d6795 --- /dev/null +++ b/spec/language_server/definition/location_link/module_access_store_state @@ -0,0 +1,79 @@ +store Header { + state title = "Mint" +} +-----------------------------------------------------------------file store.mint +component Test { + fun render : Html { +
+ <{ Header.title }> +
+ } +} +------------------------------------------------------------------file test.mint +{ + "id": 0, + "method": "initialize", + "params": { + "capabilities": { + "textDocument": { + "definition": { + "linkSupport": true + } + } + } + } +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "id": 1, + "params": { + "textDocument": { + "uri": "file://#{root_path}/test.mint" + }, + "position": { + "line": 3, + "character": 16 + } + }, + "method": "textDocument/definition" +} +-------------------------------------------------------------------------request +{ + "jsonrpc": "2.0", + "result": { + "originSelectionRange": { + "start": { + "line": 3, + "character": 16 + }, + "end": { + "line": 3, + "character": 21 + } + }, + "targetUri": "file://#{root_path}/store.mint", + "targetRange": { + "start": { + "line": 1, + "character": 2 + }, + "end": { + "line": 1, + "character": 22 + } + }, + "targetSelectionRange": { + "start": { + "line": 1, + "character": 8 + }, + "end": { + "line": 1, + "character": 13 + } + } + }, + "id": 1 +} +------------------------------------------------------------------------response diff --git a/src/ls/definition/module_access.cr b/src/ls/definition/module_access.cr new file mode 100644 index 000000000..6a67c510d --- /dev/null +++ b/src/ls/definition/module_access.cr @@ -0,0 +1,20 @@ +module Mint + module LS + class Definition < LSP::RequestMessage + def definition(node : Ast::ModuleAccess, server : Server, workspace : Workspace, stack : Array(Ast::Node)) + lookup = workspace.type_checker.lookups[node.variable]? + + if lookup + case lookup + when Ast::Property, + Ast::Constant, + Ast::Function, + Ast::State, + Ast::Get + location_link server, node.variable, lookup.name, lookup + end + end + end + end + end +end diff --git a/src/ls/definition/type_id.cr b/src/ls/definition/type_id.cr index eed5a2a24..7471dcb30 100644 --- a/src/ls/definition/type_id.cr +++ b/src/ls/definition/type_id.cr @@ -6,6 +6,7 @@ module Mint workspace.ast.enums.find(&.name.value.==(node.value)) || workspace.ast.records.find(&.name.value.==(node.value)) || workspace.ast.stores.find(&.name.value.==(node.value)) || + workspace.ast.modules.find(&.name.value.==(node.value)) || find_component(workspace, node.value) if found.nil? && (next_node = stack[1]) @@ -15,7 +16,7 @@ module Mint return if Core.ast.nodes.includes?(found) case found - when Ast::Store, Ast::Enum, Ast::Component, Ast::RecordDefinition + when Ast::Store, Ast::Enum, Ast::Component, Ast::RecordDefinition, Ast::Module location_link server, node, found.name, found end end diff --git a/src/parsers/connect_variable.cr b/src/parsers/connect_variable.cr index 62f195174..d1ab6cc7f 100644 --- a/src/parsers/connect_variable.cr +++ b/src/parsers/connect_variable.cr @@ -4,7 +4,7 @@ module Mint def connect_variable start do |start_position| - value = variable(track: false) || constant_variable + value = variable(track: false) || variable_constant next unless value diff --git a/src/parsers/constant_access.cr b/src/parsers/constant_access.cr index d981a66fe..06df8587b 100644 --- a/src/parsers/constant_access.cr +++ b/src/parsers/constant_access.cr @@ -11,11 +11,11 @@ module Mint next unless name variable = - constant_variable + variable_constant next unless variable - Ast::ModuleAccess.new( + self << Ast::ModuleAccess.new( from: start_position, variable: variable, constant: true, diff --git a/src/parsers/constant_variable.cr b/src/parsers/constant_variable.cr deleted file mode 100644 index 24fde43d6..000000000 --- a/src/parsers/constant_variable.cr +++ /dev/null @@ -1,24 +0,0 @@ -module Mint - class Parser - def constant_variable : Ast::Variable? - start do |start_position| - head = - gather { chars &.ascii_uppercase? } - - tail = - gather { chars { |char| char.ascii_uppercase? || char.ascii_number? || char == '_' } } - - next unless head || tail - - name = - "#{head}#{tail}" - - Ast::Variable.new( - from: start_position, - to: position, - input: data, - value: name) - end - end - end -end diff --git a/src/parsers/variable.cr b/src/parsers/variable.cr index 21ba86fcf..7042da0c1 100644 --- a/src/parsers/variable.cr +++ b/src/parsers/variable.cr @@ -43,7 +43,7 @@ module Mint end end - def variable_constant! : Ast::Variable + def variable_constant : Ast::Variable? start do |start_position| head = gather { chars &.ascii_uppercase? } @@ -51,7 +51,7 @@ module Mint tail = gather { chars { |char| char.ascii_uppercase? || char.ascii_number? || char == '_' } } - raise ConstantExpectedName unless head + next unless head value = "#{head}#{tail}" @@ -63,6 +63,10 @@ module Mint end end + def variable_constant! : Ast::Variable + variable_constant || raise ConstantExpectedName + end + def variable!(error : SyntaxError.class, track = true) : Ast::Variable variable(track) || raise error end