Skip to content

Commit

Permalink
Refactor how location of nodes are calculated.
Browse files Browse the repository at this point in the history
Instead of calculating the location (line, column) of a node after the fact it is now part of the parser.
  • Loading branch information
gdotdesign committed Nov 12, 2024
1 parent 2599842 commit 77f5ca5
Show file tree
Hide file tree
Showing 108 changed files with 521 additions and 525 deletions.
14 changes: 14 additions & 0 deletions benchmark/benchmark_core_parsing.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "benchmark"
require "../src/all"

module Mint
Benchmark.ips(warmup: 4.seconds, calculation: 10.seconds) do |x|
x.report("Core parsing") do
Core.files.reduce(Ast.new) do |memo, file|
Parser.parse(file.read, file.path).try do |ast|
memo.merge(ast)
end
end
end
end
end
21 changes: 10 additions & 11 deletions spec/ast/node_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,31 @@ describe Mint::Ast::Node do
}
MINT

location =
node =
Mint::Parser
.parse(example, "example.mint")
.components
.first
.functions
.first
.location

location.start.should eq({2, 2})
{node.from[1], node.from[2]}.should eq({2, 2})

# actually {4, 2} but all parsers go over by 1
location.end.should eq({4, 3})
{node.to[1], node.to[2]}.should eq({4, 3})

# First line
location.contains?(2, 1).should eq(false) # space before `f`
location.contains?(2, 3).should eq(true) # `f` of `fun`
node.contains?(2, 1).should eq(false) # space before `f`
node.contains?(2, 3).should eq(true) # `f` of `fun`

# Middle line
location.contains?(3, 0).should eq(true)
location.contains?(3, 9).should eq(true)
location.contains?(3, 1000).should eq(true)
node.contains?(3, 0).should eq(true)
node.contains?(3, 9).should eq(true)
node.contains?(3, 1000).should eq(true)

# End line
location.contains?(4, 2).should eq(true) # `}`
location.contains?(4, 3).should eq(false) # space after `}`
node.contains?(4, 2).should eq(true) # `}`
node.contains?(4, 3).should eq(false) # space after `}`
end
end
end
29 changes: 14 additions & 15 deletions src/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module Mint

getter unified_modules, unified_locales

def initialize(@type_definitions = [] of TypeDefinition,
@operators = [] of Tuple(Int64, Int64),
@keywords = [] of Tuple(Int64, Int64),
def initialize(@operators = [] of Tuple(Parser::Location, Parser::Location),
@keywords = [] of Tuple(Parser::Location, Parser::Location),
@type_definitions = [] of TypeDefinition,
@unified_modules = [] of Module,
@unified_locales = [] of Locale,
@components = [] of Component,
Expand All @@ -26,19 +26,19 @@ module Mint
end

def self.space_separated?(node1, node2)
node1.file.contents[node1.to, node2.from - node1.to].count('\n') > 1
node1.file.contents[node1.to[0], node2.from[0] - node1.to[0]].count('\n') > 1
end

def self.new_line?(node1, node2)
node1.file.contents[node1.from, node2.from - node1.from].includes?('\n')
node1.file.contents[node1.from[0], node2.from[0] - node1.from[0]].includes?('\n')
end

def new_line?(node1, node2)
start_position =
node1.from
node1.from[0]

count =
node2.to - node1.from
node2.to[0] - node1.from[0]

node1.file.contents[start_position, count].includes?('\n')
end
Expand Down Expand Up @@ -68,7 +68,7 @@ module Mint
path : String,
line : Int64
) : Array(Ast::Node)
nodes_at_path(path).select!(&.location.contains?(line, column))
nodes_at_path(path).select!(&.contains?(line, column))
end

def nodes_at_path(path : String) : Array(Ast::Node)
Expand Down Expand Up @@ -99,11 +99,10 @@ module Mint
# TODO: We may need to store each modules name node for
# future features, but for now we just store the first
comment: modules.compact_map(&.comment).first?,
from: {0_i64, 1_i64, 0_i64},
to: {0_i64, 1_i64, 0_i64},
name: modules.first.name,
comments: [] of Comment,
from: 0,
to: 0,
)
comments: [] of Comment)
end

@unified_locales =
Expand All @@ -114,9 +113,9 @@ module Mint
file: Parser::File.new(contents: "", path: ""),
fields: locales.flat_map(&.fields),
language: locales.first.language,
comment: nil,
from: 0,
to: 0)
from: {0_i64, 1_i64, 0_i64},
to: {0_i64, 1_i64, 0_i64},
comment: nil)
end

self
Expand Down
8 changes: 4 additions & 4 deletions src/ast/access.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module Mint
Dot
end

def initialize(@file : Parser::File,
def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@expression : Node,
@field : Variable,
@from : Int64,
@type : Type,
@to : Int64)
@type : Type)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/ast/argument.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module Mint
class Argument < Node
getter type, name, default

def initialize(@file : Parser::File,
def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@default : Node?,
@name : Variable,
@from : Int64,
@type : Node,
@to : Int64)
@type : Node)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/ast/array_destructuring.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module Mint
class ArrayDestructuring < Node
getter items

def initialize(@file : Parser::File,
@items : Array(Node),
@from : Int64,
@to : Int64)
def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@items : Array(Node))
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/ast/array_literal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module Mint
class ArrayLiteral < Node
getter items, type

def initialize(@file : Parser::File,
def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@items : Array(Node),
@from : Int64,
@type : Node?,
@to : Int64)
@type : Node?)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/ast/await.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module Mint
class Await < Node
getter body

def initialize(@file : Parser::File,
@from : Int64,
@body : Node,
@to : Int64)
def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@body : Node)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/ast/block.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module Mint
getter expressions

def initialize(@expressions : Array(Node),
@file : Parser::File,
@from : Int64,
@to : Int64)
@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/ast/bool_literal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module Mint
class BoolLiteral < Node
getter value

def initialize(@file : Parser::File,
@value : Bool,
@from : Int64,
@to : Int64)
def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@value : Bool)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/ast/bracket_access.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module Mint
class BracketAccess < Node
getter index, expression

def initialize(@file : Parser::File,
def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@expression : Node,
@index : Node,
@from : Int64,
@to : Int64)
@index : Node)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/ast/builtin.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module Mint
class Builtin < Node
getter value

def initialize(@file : Parser::File,
@value : String,
@from : Int64,
@to : Int64)
def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@value : String)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/ast/call.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module Mint
getter arguments, expression, await

def initialize(@arguments : Array(Field),
@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@expression : Node,
@await : Bool,
@from : Int64,
@to : Int64)
@await : Bool)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/ast/case.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module Mint

def initialize(@branches : Array(CaseBranch),
@comments : Array(Comment),
@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@condition : Node,
@from : Int64,
@to : Int64)
@condition : Node)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/ast/case_branch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module Mint
getter pattern, expression

def initialize(@expression : Node | Array(CssDefinition),
@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@pattern : Node?,
@from : Int64,
@to : Int64)
@pattern : Node?)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/ast/comment.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ module Mint
Block
end

def initialize(@next_comment : Comment?,
def initialize(@from : Parser::Location,
@next_comment : Comment?,
@to : Parser::Location,
@file : Parser::File,
@content : String,
@from : Int64,
@type : Type,
@to : Int64)
@type : Type)
end

def block?
Expand Down
4 changes: 2 additions & 2 deletions src/ast/component.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module Mint
@functions : Array(Function),
@comments : Array(Comment),
@connects : Array(Connect),
@from : Parser::Location,
@to : Parser::Location,
@states : Array(State),
@styles : Array(Style),
@file : Parser::File,
Expand All @@ -21,8 +23,6 @@ module Mint
@locales : Bool,
@global : Bool,
@async : Bool,
@from : Int64,
@to : Int64,
@name : Id)
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/ast/connect.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module Mint
getter keys, store

def initialize(@keys : Array(ConnectVariable),
@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@from : Int64,
@store : Id,
@to : Int64)
@store : Id)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/ast/connect_variable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module Mint
class ConnectVariable < Node
getter target, name

def initialize(@file : Parser::File,
def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@target : Variable?,
@name : Variable,
@from : Int64,
@to : Int64)
@name : Variable)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/ast/constant.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module Mint
class Constant < Node
getter name, expression, comment

def initialize(@file : Parser::File,
def initialize(@from : Parser::Location,
@to : Parser::Location,
@file : Parser::File,
@comment : Comment?,
@expression : Node,
@name : Variable,
@from : Int64,
@to : Int64)
@name : Variable)
end
end
end
Expand Down
Loading

0 comments on commit 77f5ca5

Please sign in to comment.