Skip to content

Commit

Permalink
Don't allow bools as variables for tuple destructuring.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign committed May 1, 2023
1 parent a5e46ec commit 1590d81
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/ast/tuple_literal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Mint
class TupleLiteral < Node
getter items

def initialize(@items : Array(Expression) | Array(BoolLiteral),
def initialize(@items : Array(Expression),
@input : Data,
@from : Int32,
@to : Int32)
Expand Down
1 change: 0 additions & 1 deletion src/parsers/case_branch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module Mint
match =
constant_access ||
enum_destructuring ||
bool_tuple_literal ||
tuple_destructuring ||
array_destructuring ||
expression
Expand Down
7 changes: 5 additions & 2 deletions src/parsers/tuple_destructuring.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module Mint
class Parser
INVALID_VARIABLE_NAMES =
["true", "false"]

def tuple_destructuring : Ast::TupleDestructuring?
start do |start_position|
head = start do
next unless char! '{'
value = tuple_destructuring || variable
value = tuple_destructuring || variable(true, INVALID_VARIABLE_NAMES)
whitespace
next if char.in?('|', '=') # Don't parse record or record update as tuple destructuring
char! ','
Expand All @@ -15,7 +18,7 @@ module Mint
next unless head

parameters = [head] &+ list(terminator: '}', separator: ',') do
tuple_destructuring || variable
tuple_destructuring || variable(true, INVALID_VARIABLE_NAMES)
end

whitespace
Expand Down
21 changes: 0 additions & 21 deletions src/parsers/tuple_literal.cr
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
module Mint
class Parser
def bool_tuple_literal
start do |start_position|
next unless char! '{'

whitespace
items = list(
terminator: '}',
separator: ','
) { bool_literal }
whitespace

next unless char! '}'

Ast::TupleLiteral.new(
from: start_position,
items: items,
to: position,
input: data)
end
end

def tuple_literal : Ast::TupleLiteral?
start do |start_position|
next unless char! '{'
Expand Down
3 changes: 2 additions & 1 deletion src/parsers/variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ module Mint
variable(track) || raise error
end

def variable(track = true) : Ast::Variable?
def variable(track = true, invalid = [] of String) : Ast::Variable?
start do |start_position|
value = gather do
next unless char.ascii_lowercase?
letters_or_numbers
end

next unless value
next if value.in?(invalid)

node = Ast::Variable.new(
from: start_position,
Expand Down

0 comments on commit 1590d81

Please sign in to comment.