Run the format tool.

This commit is contained in:
Danila Fedorin 2018-07-25 13:53:35 -07:00
parent fa00bda8ab
commit df2a00fd66
7 changed files with 570 additions and 551 deletions

View File

@ -3,11 +3,11 @@ require "./parsers.cr"
module Chalk module Chalk
module Builder module Builder
def type(type): BasicParser(Token) def type(type) : BasicParser(Token)
return TypeParser.new(type).as(BasicParser(Token)) return TypeParser.new(type).as(BasicParser(Token))
end end
def char(type): BasicParser(Token) def char(type) : BasicParser(Token)
return CharParser.new(type).as(BasicParser(Token)) return CharParser.new(type).as(BasicParser(Token))
end end
@ -15,19 +15,19 @@ module Chalk
return TransformParser.new(parser, &transform).as(BasicParser(R)) return TransformParser.new(parser, &transform).as(BasicParser(R))
end 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?)) return OptionalParser.new(parser).as(BasicParser(T?))
end 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)) return EitherParser.new(args.to_a).as(BasicParser(T))
end 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))) return ManyParser.new(parser).as(BasicParser(Array(T)))
end 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))) return DelimitedParser.new(parser, delimiter).as(BasicParser(Array(T)))
end end

View File

@ -52,11 +52,11 @@ module Chalk
call = create_call(expr_place) call = create_call(expr_place)
atom = either(literal, call, id) 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)), either(type(TokenType::OpAdd), type(TokenType::OpSub)),
type(TokenType::OpXor), type(TokenType::OpXor),
type(TokenType::OpAnd), type(TokenType::OpAnd),
type(TokenType::OpOr) ] type(TokenType::OpOr)]
expr = create_op_exprs(atom, ops) expr = create_op_exprs(atom, ops)
expr_place.parser = expr expr_place.parser = expr

View File

@ -1,12 +1,12 @@
require "./builder.cr" require "./builder.cr"
module Chalk module Chalk
abstract class BasicParser(T) abstract class BasicParser(T)
abstract def parse?(tokens : Array(Token), abstract def parse?(tokens : Array(Token),
index : Int64): Tuple(T, Int64)? index : Int64) : Tuple(T, Int64)?
def parse(tokens : Array(Token), def parse(tokens : Array(Token),
index : Int64): Tuple(T, Int64) index : Int64) : Tuple(T, Int64)
return parse?(tokens, index).not_nil! return parse?(tokens, index).not_nil!
end end
@ -14,10 +14,8 @@ module Chalk
return TransformParser.new(self, &transform).as(BasicParser(R)) return TransformParser.new(self, &transform).as(BasicParser(R))
end end
def then(other : BasicParser(R)): BasicParser(Array(T | R)) forall R def then(other : BasicParser(R)) : BasicParser(Array(T | R)) forall R
return NextParser return NextParser.new(self, other).as(BasicParser(Array(T | R)))
.new(self, other)
.as(BasicParser(Array(T | R)))
end end
end end
@ -28,7 +26,7 @@ module Chalk
def parse?(tokens, index) def parse?(tokens, index)
return nil unless index < tokens.size return nil unless index < tokens.size
return nil unless tokens[index].type == @type return nil unless tokens[index].type == @type
return { tokens[index], index + 1} return {tokens[index], index + 1}
end end
end end
@ -40,7 +38,7 @@ module Chalk
return nil unless index < tokens.size return nil unless index < tokens.size
return nil unless (tokens[index].type == TokenType::Any) && return nil unless (tokens[index].type == TokenType::Any) &&
tokens[index].string[0] == @char tokens[index].string[0] == @char
return { tokens[index], index + 1} return {tokens[index], index + 1}
end end
end end
@ -50,7 +48,7 @@ module Chalk
def parse?(tokens, index) def parse?(tokens, index)
if parsed = @parser.parse?(tokens, index) if parsed = @parser.parse?(tokens, index)
return { @block.call(parsed[0]), parsed[1] } return {@block.call(parsed[0]), parsed[1]}
end end
return nil return nil
end end
@ -62,9 +60,9 @@ module Chalk
def parse?(tokens, index) def parse?(tokens, index)
if parsed = @parser.parse?(tokens, index) if parsed = @parser.parse?(tokens, index)
return { parsed[0], parsed[1] } return {parsed[0], parsed[1]}
end end
return { nil, index } return {nil, index}
end end
end end
@ -92,7 +90,7 @@ module Chalk
item, index = parsed item, index = parsed
many << item many << item
end end
return { many, index } return {many, index}
end end
end end
@ -103,7 +101,7 @@ module Chalk
def parse?(tokens, index) def parse?(tokens, index)
array = [] of T array = [] of T
first = @parser.parse?(tokens, index) first = @parser.parse?(tokens, index)
return { array, index } unless first return {array, index} unless first
first_value, index = first first_value, index = first
array << first_value array << first_value
while delimiter = @delimiter.parse?(tokens, index) while delimiter = @delimiter.parse?(tokens, index)
@ -114,7 +112,7 @@ module Chalk
array << new_value array << new_value
end end
return { array, index } return {array, index}
end end
end end
@ -133,7 +131,7 @@ module Chalk
array = Array(T | R).new array = Array(T | R).new
array << first_value << second_value array << first_value << second_value
return { array, index } return {array, index}
end end
end end

View File

@ -16,19 +16,25 @@ module Chalk
class TreeId < Tree class TreeId < Tree
property id : String property id : String
def initialize(@id : String) end
def initialize(@id : String)
end
end end
class TreeLit < Tree class TreeLit < Tree
property lit : Int64 property lit : Int64
def initialize(@lit : Int64) end
def initialize(@lit : Int64)
end
end end
class TreeCall < Tree class TreeCall < Tree
property name : String property name : String
property params : Array(Tree) property params : Array(Tree)
def initialize(@name : String, @params : Array(Tree)) end def initialize(@name : String, @params : Array(Tree))
end
def accept(v : Visitor) def accept(v : Visitor)
v.visit(self) v.visit(self)
@params.each &.accept(v) @params.each &.accept(v)
@ -40,7 +46,9 @@ module Chalk
property op : TokenType property op : TokenType
property left : Tree property left : Tree
property right : 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) def accept(v : Visitor)
v.visit(self) v.visit(self)
@ -51,7 +59,8 @@ module Chalk
end end
class TreeBlock < Tree class TreeBlock < Tree
def initialize(@children : Array(Tree)) end def initialize(@children : Array(Tree))
end
def accept(v : Visitor) def accept(v : Visitor)
v.visit(self) v.visit(self)
@ -65,7 +74,9 @@ module Chalk
property params : Array(String) property params : Array(String)
property block : Tree 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) def accept(v : Visitor)
v.visit(self) v.visit(self)
@block.accept(v) @block.accept(v)
@ -77,7 +88,9 @@ module Chalk
property name : String property name : String
property expr : Tree property expr : Tree
def initialize(@name : String, @expr : Tree) end def initialize(@name : String, @expr : Tree)
end
def accept(v : Visitor) def accept(v : Visitor)
v.visit(self) v.visit(self)
@expr.accept(v) @expr.accept(v)
@ -89,7 +102,9 @@ module Chalk
property name : String property name : String
property expr : Tree property expr : Tree
def initialize(@name : String, @expr : Tree) end def initialize(@name : String, @expr : Tree)
end
def accept(v : Visitor) def accept(v : Visitor)
v.visit(self) v.visit(self)
@expr.accept(v) @expr.accept(v)
@ -102,7 +117,9 @@ module Chalk
property block : Tree property block : Tree
property otherwise : 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) def accept(v : Visitor)
v.visit(self) v.visit(self)
@condition.accept(v) @condition.accept(v)
@ -116,7 +133,9 @@ module Chalk
property condition : Tree property condition : Tree
property block : Tree property block : Tree
def initialize(@condition : Tree, @block : Tree) end def initialize(@condition : Tree, @block : Tree)
end
def accept(v : Visitor) def accept(v : Visitor)
v.visit(self) v.visit(self)
@condition.accept(v) @condition.accept(v)
@ -128,7 +147,9 @@ module Chalk
class TreeReturn < Tree class TreeReturn < Tree
property rvalue : Tree property rvalue : Tree
def initialize(@rvalue : Tree) end def initialize(@rvalue : Tree)
end
def accept(v : Visitor) def accept(v : Visitor)
v.visit(self) v.visit(self)
@rvalue.accept(v) @rvalue.accept(v)