Run the format tool.
This commit is contained in:
parent
fa00bda8ab
commit
df2a00fd66
|
@ -3,11 +3,11 @@ require "./parsers.cr"
|
|||
|
||||
module Chalk
|
||||
module Builder
|
||||
def type(type): BasicParser(Token)
|
||||
def type(type) : BasicParser(Token)
|
||||
return TypeParser.new(type).as(BasicParser(Token))
|
||||
end
|
||||
|
||||
def char(type): BasicParser(Token)
|
||||
def char(type) : BasicParser(Token)
|
||||
return CharParser.new(type).as(BasicParser(Token))
|
||||
end
|
||||
|
||||
|
@ -15,19 +15,19 @@ module Chalk
|
|||
return TransformParser.new(parser, &transform).as(BasicParser(R))
|
||||
end
|
||||
|
||||
def optional(parser : BasicParser(T)): BasicParser(T?) forall T
|
||||
def optional(parser : BasicParser(T)) : BasicParser(T?) forall T
|
||||
return OptionalParser.new(parser).as(BasicParser(T?))
|
||||
end
|
||||
|
||||
def either(*args : BasicParser(T)): BasicParser(T) forall T
|
||||
def either(*args : BasicParser(T)) : BasicParser(T) forall T
|
||||
return EitherParser.new(args.to_a).as(BasicParser(T))
|
||||
end
|
||||
|
||||
def many(parser : BasicParser(T)): BasicParser(Array(T)) forall T
|
||||
def many(parser : BasicParser(T)) : BasicParser(Array(T)) forall T
|
||||
return ManyParser.new(parser).as(BasicParser(Array(T)))
|
||||
end
|
||||
|
||||
def delimited(parser : BasicParser(T), delimiter : BasicParser(R)): BasicParser(Array(T)) forall T, R
|
||||
def delimited(parser : BasicParser(T), delimiter : BasicParser(R)) : BasicParser(Array(T)) forall T, R
|
||||
return DelimitedParser.new(parser, delimiter).as(BasicParser(Array(T)))
|
||||
end
|
||||
|
||||
|
|
|
@ -52,11 +52,11 @@ module Chalk
|
|||
call = create_call(expr_place)
|
||||
atom = either(literal, call, id)
|
||||
|
||||
ops = [ either(type(TokenType::OpMul), type(TokenType::OpDiv)),
|
||||
ops = [either(type(TokenType::OpMul), type(TokenType::OpDiv)),
|
||||
either(type(TokenType::OpAdd), type(TokenType::OpSub)),
|
||||
type(TokenType::OpXor),
|
||||
type(TokenType::OpAnd),
|
||||
type(TokenType::OpOr) ]
|
||||
type(TokenType::OpOr)]
|
||||
expr = create_op_exprs(atom, ops)
|
||||
expr_place.parser = expr
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
require "./builder.cr"
|
||||
|
||||
module Chalk
|
||||
|
||||
abstract class BasicParser(T)
|
||||
abstract def parse?(tokens : Array(Token),
|
||||
index : Int64): Tuple(T, Int64)?
|
||||
index : Int64) : Tuple(T, Int64)?
|
||||
|
||||
def parse(tokens : Array(Token),
|
||||
index : Int64): Tuple(T, Int64)
|
||||
index : Int64) : Tuple(T, Int64)
|
||||
return parse?(tokens, index).not_nil!
|
||||
end
|
||||
|
||||
|
@ -14,10 +14,8 @@ module Chalk
|
|||
return TransformParser.new(self, &transform).as(BasicParser(R))
|
||||
end
|
||||
|
||||
def then(other : BasicParser(R)): BasicParser(Array(T | R)) forall R
|
||||
return NextParser
|
||||
.new(self, other)
|
||||
.as(BasicParser(Array(T | R)))
|
||||
def then(other : BasicParser(R)) : BasicParser(Array(T | R)) forall R
|
||||
return NextParser.new(self, other).as(BasicParser(Array(T | R)))
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -28,7 +26,7 @@ module Chalk
|
|||
def parse?(tokens, index)
|
||||
return nil unless index < tokens.size
|
||||
return nil unless tokens[index].type == @type
|
||||
return { tokens[index], index + 1}
|
||||
return {tokens[index], index + 1}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -40,7 +38,7 @@ module Chalk
|
|||
return nil unless index < tokens.size
|
||||
return nil unless (tokens[index].type == TokenType::Any) &&
|
||||
tokens[index].string[0] == @char
|
||||
return { tokens[index], index + 1}
|
||||
return {tokens[index], index + 1}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -50,7 +48,7 @@ module Chalk
|
|||
|
||||
def parse?(tokens, index)
|
||||
if parsed = @parser.parse?(tokens, index)
|
||||
return { @block.call(parsed[0]), parsed[1] }
|
||||
return {@block.call(parsed[0]), parsed[1]}
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
@ -62,9 +60,9 @@ module Chalk
|
|||
|
||||
def parse?(tokens, index)
|
||||
if parsed = @parser.parse?(tokens, index)
|
||||
return { parsed[0], parsed[1] }
|
||||
return {parsed[0], parsed[1]}
|
||||
end
|
||||
return { nil, index }
|
||||
return {nil, index}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -92,7 +90,7 @@ module Chalk
|
|||
item, index = parsed
|
||||
many << item
|
||||
end
|
||||
return { many, index }
|
||||
return {many, index}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -103,7 +101,7 @@ module Chalk
|
|||
def parse?(tokens, index)
|
||||
array = [] of T
|
||||
first = @parser.parse?(tokens, index)
|
||||
return { array, index } unless first
|
||||
return {array, index} unless first
|
||||
first_value, index = first
|
||||
array << first_value
|
||||
while delimiter = @delimiter.parse?(tokens, index)
|
||||
|
@ -114,7 +112,7 @@ module Chalk
|
|||
array << new_value
|
||||
end
|
||||
|
||||
return { array, index }
|
||||
return {array, index}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -133,7 +131,7 @@ module Chalk
|
|||
|
||||
array = Array(T | R).new
|
||||
array << first_value << second_value
|
||||
return { array, index }
|
||||
return {array, index}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -16,19 +16,25 @@ module Chalk
|
|||
|
||||
class TreeId < Tree
|
||||
property id : String
|
||||
def initialize(@id : String) end
|
||||
|
||||
def initialize(@id : String)
|
||||
end
|
||||
end
|
||||
|
||||
class TreeLit < Tree
|
||||
property lit : Int64
|
||||
def initialize(@lit : Int64) end
|
||||
|
||||
def initialize(@lit : Int64)
|
||||
end
|
||||
end
|
||||
|
||||
class TreeCall < Tree
|
||||
property name : String
|
||||
property params : Array(Tree)
|
||||
|
||||
def initialize(@name : String, @params : Array(Tree)) end
|
||||
def initialize(@name : String, @params : Array(Tree))
|
||||
end
|
||||
|
||||
def accept(v : Visitor)
|
||||
v.visit(self)
|
||||
@params.each &.accept(v)
|
||||
|
@ -40,7 +46,9 @@ module Chalk
|
|||
property op : TokenType
|
||||
property left : Tree
|
||||
property right : Tree
|
||||
def initialize(@op : TokenType, @left : Tree, @right : Tree) end
|
||||
|
||||
def initialize(@op : TokenType, @left : Tree, @right : Tree)
|
||||
end
|
||||
|
||||
def accept(v : Visitor)
|
||||
v.visit(self)
|
||||
|
@ -51,7 +59,8 @@ module Chalk
|
|||
end
|
||||
|
||||
class TreeBlock < Tree
|
||||
def initialize(@children : Array(Tree)) end
|
||||
def initialize(@children : Array(Tree))
|
||||
end
|
||||
|
||||
def accept(v : Visitor)
|
||||
v.visit(self)
|
||||
|
@ -65,7 +74,9 @@ module Chalk
|
|||
property params : Array(String)
|
||||
property block : Tree
|
||||
|
||||
def initialize(@name : String, @params : Array(String), @block : Tree) end
|
||||
def initialize(@name : String, @params : Array(String), @block : Tree)
|
||||
end
|
||||
|
||||
def accept(v : Visitor)
|
||||
v.visit(self)
|
||||
@block.accept(v)
|
||||
|
@ -77,7 +88,9 @@ module Chalk
|
|||
property name : String
|
||||
property expr : Tree
|
||||
|
||||
def initialize(@name : String, @expr : Tree) end
|
||||
def initialize(@name : String, @expr : Tree)
|
||||
end
|
||||
|
||||
def accept(v : Visitor)
|
||||
v.visit(self)
|
||||
@expr.accept(v)
|
||||
|
@ -89,7 +102,9 @@ module Chalk
|
|||
property name : String
|
||||
property expr : Tree
|
||||
|
||||
def initialize(@name : String, @expr : Tree) end
|
||||
def initialize(@name : String, @expr : Tree)
|
||||
end
|
||||
|
||||
def accept(v : Visitor)
|
||||
v.visit(self)
|
||||
@expr.accept(v)
|
||||
|
@ -102,7 +117,9 @@ module Chalk
|
|||
property block : Tree
|
||||
property otherwise : Tree?
|
||||
|
||||
def initialize(@condition : Tree, @block : Tree, @otherwise : Tree? = nil) end
|
||||
def initialize(@condition : Tree, @block : Tree, @otherwise : Tree? = nil)
|
||||
end
|
||||
|
||||
def accept(v : Visitor)
|
||||
v.visit(self)
|
||||
@condition.accept(v)
|
||||
|
@ -116,7 +133,9 @@ module Chalk
|
|||
property condition : Tree
|
||||
property block : Tree
|
||||
|
||||
def initialize(@condition : Tree, @block : Tree) end
|
||||
def initialize(@condition : Tree, @block : Tree)
|
||||
end
|
||||
|
||||
def accept(v : Visitor)
|
||||
v.visit(self)
|
||||
@condition.accept(v)
|
||||
|
@ -128,7 +147,9 @@ module Chalk
|
|||
class TreeReturn < Tree
|
||||
property rvalue : Tree
|
||||
|
||||
def initialize(@rvalue : Tree) end
|
||||
def initialize(@rvalue : Tree)
|
||||
end
|
||||
|
||||
def accept(v : Visitor)
|
||||
v.visit(self)
|
||||
@rvalue.accept(v)
|
||||
|
|
Loading…
Reference in New Issue
Block a user