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

Refactor how location of nodes are calculated. #703

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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.line, node.from.column}.should eq({2, 2})

# actually {4, 2} but all parsers go over by 1
location.end.should eq({4, 3})
{node.to.line, node.to.column}.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
47 changes: 16 additions & 31 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 {from: Parser::Location, to: Parser::Location},
@keywords = [] of {from: Parser::Location, to: Parser::Location},
@type_definitions = [] of TypeDefinition,
@unified_modules = [] of Module,
@unified_locales = [] of Locale,
@components = [] of Component,
Expand All @@ -21,26 +21,12 @@ module Mint
@nodes = [] of Node)
end

def main : Component?
@components.find(&.name.value.==("Main"))
end

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

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

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

count =
node2.to - node1.from

node1.file.contents[start_position, count].includes?('\n')
def main : Component?
@components.find(&.name.value.==("Main"))
end

def merge(ast) : self
Expand Down Expand Up @@ -68,7 +54,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 All @@ -93,17 +79,16 @@ module Mint
.group_by(&.name.value)
.map do |_, modules|
Module.new(
functions: modules.flat_map(&.functions),
constants: modules.flat_map(&.constants),
file: Parser::File.new(contents: "", path: ""),
# 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?,
file: Parser::File.new(contents: "", path: ""),
functions: modules.flat_map(&.functions),
constants: modules.flat_map(&.constants),
from: Parser::Location.new,
to: Parser::Location.new,
name: modules.first.name,
comments: [] of Comment,
from: 0,
to: 0,
)
comments: [] of Comment)
end

@unified_locales =
Expand All @@ -114,9 +99,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: Parser::Location.new,
to: Parser::Location.new,
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
Loading