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

Update Ast::Constant to use an Ast::Variable for its name #594

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions spec/parsers/variable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/ast/constant.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Mint

def initialize(@value : Expression,
@comment : Comment?,
@name : String,
@name : Variable,
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
2 changes: 1 addition & 1 deletion src/compilers/component.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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};")
Expand Down
2 changes: 1 addition & 1 deletion src/compilers/variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Sija marked this conversation as resolved.
Show resolved Hide resolved
end

if store
Expand Down
2 changes: 1 addition & 1 deletion src/ls/code_actions/module_actions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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] }

Expand Down
2 changes: 1 addition & 1 deletion src/ls/code_actions/provider_actions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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] }
Expand Down
4 changes: 2 additions & 2 deletions src/ls/completion_item/constant.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 1 addition & 10 deletions src/parsers/constant.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/parsers/variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ module Mint
end
end

def variable_constant! : Ast::Variable
gdotdesign marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
8 changes: 4 additions & 4 deletions src/type_checker/scope.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]? ||
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/type_checkers/connect.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/type_checkers/module_access.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down