From 9f57c4c8f49d4770611f01c8dc17e100d3b20024 Mon Sep 17 00:00:00 2001 From: Jon Sully Date: Fri, 5 May 2023 17:19:05 +0100 Subject: [PATCH 1/2] Update `Ast::Constant` to use an `Ast::Variable` for its name --- spec/parsers/variable_spec.cr | 18 ++++++++++++++++++ src/ast/constant.cr | 2 +- src/compilers/component.cr | 2 +- src/compilers/variable.cr | 2 +- src/ls/code_actions/module_actions.cr | 2 +- src/ls/code_actions/provider_actions.cr | 2 +- src/ls/completion_item/constant.cr | 4 ++-- src/parsers/constant.cr | 11 +---------- src/parsers/variable.cr | 20 ++++++++++++++++++++ src/type_checker/scope.cr | 8 ++++---- src/type_checkers/connect.cr | 2 +- src/type_checkers/module_access.cr | 6 +++--- 12 files changed, 54 insertions(+), 25 deletions(-) diff --git a/spec/parsers/variable_spec.cr b/spec/parsers/variable_spec.cr index fe3467e92..bcf4d0232 100644 --- a/spec/parsers/variable_spec.cr +++ b/spec/parsers/variable_spec.cr @@ -56,3 +56,21 @@ describe "Variable With Dashes!" do expect_error ".", Mint::SyntaxError expect_error "???", Mint::SyntaxError end + +describe "Variable Constant!" do + subject variable_constant! + + expect_ok "A" + expect_ok "ASD" + expect_ok "ASD_ASD_ASD" + expect_ok "ASD_ASD__ASD" + expect_ok "ASD_ASD________ASD" + expect_ok "ASD_" + + expect_error " ", Mint::Parser::ConstantExpectedName + expect_error ".", Mint::Parser::ConstantExpectedName + expect_error "_", Mint::Parser::ConstantExpectedName + expect_error "???", Mint::Parser::ConstantExpectedName + expect_error "_ASD", Mint::Parser::ConstantExpectedName + expect_error "1ASD", Mint::Parser::ConstantExpectedName +end diff --git a/src/ast/constant.cr b/src/ast/constant.cr index 5b72bd0f5..7c3e0edfa 100644 --- a/src/ast/constant.cr +++ b/src/ast/constant.cr @@ -5,7 +5,7 @@ module Mint def initialize(@value : Expression, @comment : Comment?, - @name : String, + @name : Variable, @input : Data, @from : Int32, @to : Int32) diff --git a/src/compilers/component.cr b/src/compilers/component.cr index 7759fe3a2..85e0a0874 100644 --- a/src/compilers/component.cr +++ b/src/compilers/component.cr @@ -122,7 +122,7 @@ module Mint name = js.variable_of(key) case - when store.constants.any?(&.name.==(original)), + when store.constants.any?(&.name.value.==(original)), store.gets.any?(&.name.value.==(original)), store.states.find(&.name.value.==(original)) memo << js.get(name, "return #{store_name}.#{id};") diff --git a/src/compilers/variable.cr b/src/compilers/variable.cr index f34b2fe6f..55b92ae1d 100644 --- a/src/compilers/variable.cr +++ b/src/compilers/variable.cr @@ -20,7 +20,7 @@ module Mint when Ast::Function then entity.name.value when Ast::State then entity.name.value when Ast::Get then entity.name.value - when Ast::Constant then entity.name + when Ast::Constant then entity.name.value end if store diff --git a/src/ls/code_actions/module_actions.cr b/src/ls/code_actions/module_actions.cr index 0c2b39393..7b72ad1d1 100644 --- a/src/ls/code_actions/module_actions.cr +++ b/src/ls/code_actions/module_actions.cr @@ -12,7 +12,7 @@ module Mint .map(&.from) # Reorder by name and the appropriate order from the original order - (node.constants.sort_by(&.name) + + (node.constants.sort_by(&.name.value) + node.functions.sort_by(&.name.value)) .each_with_index { |entity, index| entity.from = order[index] } diff --git a/src/ls/code_actions/provider_actions.cr b/src/ls/code_actions/provider_actions.cr index 743a5b859..170b3aa73 100644 --- a/src/ls/code_actions/provider_actions.cr +++ b/src/ls/code_actions/provider_actions.cr @@ -13,7 +13,7 @@ module Mint # Reorder by name and the appropriate order from the original order (node.states.sort_by(&.name.value) + - node.constants.sort_by(&.name) + + node.constants.sort_by(&.name.value) + node.gets.sort_by(&.name.value) + node.functions.sort_by(&.name.value)) .each_with_index { |entity, index| entity.from = order[index] } diff --git a/src/ls/completion_item/constant.cr b/src/ls/completion_item/constant.cr index 3748a0e4a..f175ec0db 100644 --- a/src/ls/completion_item/constant.cr +++ b/src/ls/completion_item/constant.cr @@ -4,9 +4,9 @@ module Mint def completion_item(node : Ast::Constant, parent_name : Ast::TypeId? = nil) : LSP::CompletionItem name = if parent_name - "#{parent_name.value}:#{node.name}" + "#{parent_name.value}:#{node.name.value}" else - node.name + node.name.value end LSP::CompletionItem.new( diff --git a/src/parsers/constant.cr b/src/parsers/constant.cr index a83bfcc32..afe96bd84 100644 --- a/src/parsers/constant.cr +++ b/src/parsers/constant.cr @@ -12,16 +12,7 @@ module Mint next unless keyword "const" whitespace - head = - gather { chars &.ascii_uppercase? } - - tail = - gather { chars { |char| char.ascii_uppercase? || char.ascii_number? || char == '_' } } - - raise ConstantExpectedName unless head || tail - - name = - "#{head}#{tail}" + name = variable_constant! whitespace char '=', ConstantExpectedEqualSign diff --git a/src/parsers/variable.cr b/src/parsers/variable.cr index 85a3f5dbb..738f972f3 100644 --- a/src/parsers/variable.cr +++ b/src/parsers/variable.cr @@ -41,6 +41,26 @@ module Mint end end + def variable_constant! : Ast::Variable + start do |start_position| + head = + gather { chars &.ascii_uppercase? } + + tail = + gather { chars { |char| char.ascii_uppercase? || char.ascii_number? || char == '_' } } + + raise ConstantExpectedName unless head + + value = "#{head}#{tail}" + + Ast::Variable.new( + from: start_position, + value: value, + to: position, + input: data) + end + end + def variable!(error : SyntaxError.class, track = true) : Ast::Variable variable(track) || raise error end diff --git a/src/type_checker/scope.cr b/src/type_checker/scope.cr index 6b444be73..37fab83dc 100644 --- a/src/type_checker/scope.cr +++ b/src/type_checker/scope.cr @@ -163,7 +163,7 @@ module Mint def find(variable : String, node : Ast::Module) node.functions.find(&.name.value.==(variable)) || - node.constants.find(&.name.==(variable)) + node.constants.find(&.name.value.==(variable)) end def find(variable : String, node : Ast::Store) @@ -191,7 +191,7 @@ module Mint node.gets.find(&.name.value.==(variable)) || node.properties.find(&.name.value.==(variable)) || node.states.find(&.name.value.==(variable)) || - node.constants.find(&.name.==(variable)) || + node.constants.find(&.name.value.==(variable)) || refs(component)[variable]? || store_constants(component)[variable]? || store_states(component)[variable]? || @@ -200,7 +200,7 @@ module Mint end def find(variable : String, node : Ast::Suite) - node.constants.find(&.name.==(variable)) + node.constants.find(&.name.value.==(variable)) end def find(variable : String, node : Ast::Node) @@ -336,7 +336,7 @@ module Mint item.keys.each do |key| store .constants - .find(&.name.==(key.variable.value)) + .find(&.name.value.==(key.variable.value)) .try do |function| memo[(key.name || key.variable).value] = function end diff --git a/src/type_checkers/connect.cr b/src/type_checkers/connect.cr index 853da6fa3..1bc68eb7c 100644 --- a/src/type_checkers/connect.cr +++ b/src/type_checkers/connect.cr @@ -20,7 +20,7 @@ module Mint store.functions.find(&.name.value.==(key_value)) || store.states.find(&.name.value.==(key_value)) || store.gets.find(&.name.value.==(key_value)) || - store.constants.find(&.name.==(key_value)) + store.constants.find(&.name.value.==(key_value)) raise ConnectNotFoundMember, { "key" => key_value, diff --git a/src/type_checkers/module_access.cr b/src/type_checkers/module_access.cr index 169866ffc..a6e4fc235 100644 --- a/src/type_checkers/module_access.cr +++ b/src/type_checkers/module_access.cr @@ -41,18 +41,18 @@ module Mint end when Ast::Module entity.functions.find(&.name.value.==(variable_value)) || - entity.constants.find(&.name.==(variable_value)) + entity.constants.find(&.name.value.==(variable_value)) when Ast::Component entity.properties.find(&.name.value.==(variable_value)) || entity.functions.find(&.name.value.==(variable_value)) || entity.states.find(&.name.value.==(variable_value)) || - entity.constants.find(&.name.==(variable_value)) || + entity.constants.find(&.name.value.==(variable_value)) || entity.gets.find(&.name.value.==(variable_value)) when Ast::Store entity.functions.find(&.name.value.==(variable_value)) || entity.states.find(&.name.value.==(variable_value)) || entity.gets.find(&.name.value.==(variable_value)) || - entity.constants.find(&.name.==(variable_value)) + entity.constants.find(&.name.value.==(variable_value)) else raise ModuleAccessNotFoundModule, { "name" => name.value, From 824f52d890588505659b617f3f1f762b64a18819 Mon Sep 17 00:00:00 2001 From: Jon Sully Date: Fri, 5 May 2023 20:38:25 +0100 Subject: [PATCH 2/2] Simplify case statement in Ast::Variable compile method --- src/compilers/variable.cr | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/compilers/variable.cr b/src/compilers/variable.cr index 55b92ae1d..01a3b042d 100644 --- a/src/compilers/variable.cr +++ b/src/compilers/variable.cr @@ -17,10 +17,8 @@ module Mint name = case entity - when Ast::Function then entity.name.value - when Ast::State then entity.name.value - when Ast::Get then entity.name.value - when Ast::Constant then entity.name.value + when Ast::Function, Ast::State, Ast::Get, Ast::Constant + entity.name.value end if store