Skip to content

Commit

Permalink
Fixed EOF bug
Browse files Browse the repository at this point in the history
  • Loading branch information
aleferri committed Oct 30, 2023
1 parent d519eb2 commit 348a833
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
23 changes: 12 additions & 11 deletions cmd/casmeleon/AssemblyStream.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/aleferri/casmeleon/pkg/text"
)

//IdentifySymbol in the casm language
// IdentifySymbol in the casm language
func IdentifySymbol(s []rune, fileOffset uint32, count uint32) text.Symbol {
if unicode.IsDigit(s[0]) {
return text.SymbolOf(fileOffset, count, string(s), text.Number)
Expand Down Expand Up @@ -38,15 +38,15 @@ func IdentifySymbol(s []rune, fileOffset uint32, count uint32) text.Symbol {
return text.SymbolOf(fileOffset, count, str, id)
}

//AssemblyStream is the stream of the symbols
// AssemblyStream is the stream of the symbols
type AssemblyStream struct {
parent *AssemblyStream
source *bufio.Reader
buffer []text.Symbol
repo *text.Source
}

//MakeRootStream for the parser
// MakeRootStream for the parser
func MakeRootStream(source *bufio.Reader, repo *text.Source) parser.Stream {
return &AssemblyStream{parent: nil, source: source, repo: repo, buffer: []text.Symbol{}}
}
Expand All @@ -55,11 +55,16 @@ func MakeChildStream(source *bufio.Reader, repo *text.Source, parent *AssemblySt
return &AssemblyStream{parent: parent, source: source, repo: repo, buffer: []text.Symbol{}}
}

//Buffer ensure that the buffer of the stream contains at least 1 element
// Buffer ensure that the buffer of the stream contains at least 1 element
func (s *AssemblyStream) Buffer() {
for len(s.buffer) == 0 {
line, ioErr := s.source.ReadBytes('\n')

if ioErr != nil {
s.buffer = append(s.buffer, text.SymbolEmpty(s.repo.FileIndex()).WithID(text.EOF))
return
}

runes := bytes.Runes(line)

temps, _ := scanner.FastScan(runes, true, scanFollowMap)
Expand All @@ -74,14 +79,10 @@ func (s *AssemblyStream) Buffer() {
s.buffer = append(s.buffer, sym)
}
}

if ioErr != nil {
s.buffer = append(s.buffer, text.SymbolEmpty(s.repo.FileIndex()).WithID(text.EOF))
}
}
}

//Next symbol in the internal buffer
// Next symbol in the internal buffer
func (s *AssemblyStream) Next() text.Symbol {
s.Buffer()

Expand All @@ -90,14 +91,14 @@ func (s *AssemblyStream) Next() text.Symbol {
return result
}

//Peek the symbol from the internal buffer
// Peek the symbol from the internal buffer
func (s *AssemblyStream) Peek() text.Symbol {
s.Buffer()

return s.buffer[0]
}

//Source of the stream
// Source of the stream
func (s *AssemblyStream) Source() *text.Source {
return s.repo
}
12 changes: 12 additions & 0 deletions cmd/casmeleon/Main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func ParseASMFile(lang casm.Language, sourceFile string) (*AssemblyProgram, erro
}
return nil, errors.New("Error during compilation")
}

parser.ConsumeAll(stream, text.EOL)
}

Expand All @@ -137,7 +138,10 @@ func main() {
flag.StringVar(&langFileName, "lang", ".", "-lang=langfile")
var debugMode bool
flag.BoolVar(&debugMode, "debug", false, "-debug=true|false")
var exportAssembly string
flag.StringVar(&exportAssembly, "export", "bin", "-export=bin|hex")
flag.Parse()

tUI := ui.NewConsole(false, false)
if strings.EqualFold(langFileName, ".") {
tUI.ReportError("missing -lang=langfile", true)
Expand Down Expand Up @@ -179,8 +183,16 @@ func main() {
for _, f := range flag.Args() {
if !strings.HasPrefix(f, "-") {

if debugMode {
fmt.Println("Parsing " + f)
}

program, errAsm := ParseASMFile(lang, f)

if debugMode {
fmt.Println("End parsing")
}

if errAsm != nil {
fmt.Println(errAsm.Error())
break
Expand Down
32 changes: 16 additions & 16 deletions pkg/parser/Match.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"github.com/aleferri/casmeleon/pkg/text"
)

//Match a generic rule in the provided stream
// Match a generic rule in the provided stream
type Match func(stream Stream) (CSTNode, error)

//Accept symbol of type sym
// Accept symbol of type sym
func Accept(stream Stream, sym uint32) (text.Symbol, bool) {
v := stream.Peek()
if v.ID() == sym {
Expand All @@ -16,13 +16,13 @@ func Accept(stream Stream, sym uint32) (text.Symbol, bool) {
return v, false
}

//Consume symbol of type sym
// Consume symbol of type sym
func Consume(stream Stream, sym uint32) bool {
_, c := Accept(stream, sym)
return c
}

//ConsumeAll symbols of type sym
// ConsumeAll symbols of type sym
func ConsumeAll(stream Stream, sym uint32) int {
i := 0
for Consume(stream, sym) {
Expand All @@ -31,7 +31,7 @@ func ConsumeAll(stream Stream, sym uint32) int {
return i
}

//Require accept a symbol of type sym or return an error if the symbol is not the accepted symbol type
// Require accept a symbol of type sym or return an error if the symbol is not the accepted symbol type
func Require(stream Stream, sym uint32) (text.Symbol, error) {
v, c := Accept(stream, sym)
if c {
Expand All @@ -40,13 +40,13 @@ func Require(stream Stream, sym uint32) (text.Symbol, error) {
return v, ExpectedSymbol(v, "Unexpected '%s', was expecting: %s", sym)
}

//Expect consume a symbol of type sym or return an error if the symbol is not the accepted symbol type
// Expect consume a symbol of type sym or return an error if the symbol is not the accepted symbol type
func Expect(stream Stream, sym uint32) error {
_, e := Require(stream, sym)
return e
}

//AcceptAny accept any of the proposed symbols
// AcceptAny accept any of the proposed symbols
func AcceptAny(stream Stream, syms ...uint32) (text.Symbol, bool) {
peek := stream.Peek()
for _, sym := range syms {
Expand All @@ -57,13 +57,13 @@ func AcceptAny(stream Stream, syms ...uint32) (text.Symbol, bool) {
return peek, false
}

//ConsumeAny symbol of type syms
// ConsumeAny symbol of type syms
func ConsumeAny(stream Stream, syms ...uint32) bool {
_, c := AcceptAny(stream, syms...)
return c
}

//RequireAny of the listed symbols
// RequireAny of the listed symbols
func RequireAny(stream Stream, syms ...uint32) (text.Symbol, error) {
v, c := AcceptAny(stream, syms...)
if !c {
Expand All @@ -72,13 +72,13 @@ func RequireAny(stream Stream, syms ...uint32) (text.Symbol, error) {
return v, nil
}

//ExpectAny of the listed symbols
// ExpectAny of the listed symbols
func ExpectAny(stream Stream, syms ...uint32) error {
_, err := RequireAny(stream, syms...)
return err
}

//RequireSequence specified by caller
// RequireSequence specified by caller
func RequireSequence(stream Stream, seq ...uint32) ([]text.Symbol, error) {
acc := []text.Symbol{}
for _, m := range seq {
Expand All @@ -91,7 +91,7 @@ func RequireSequence(stream Stream, seq ...uint32) ([]text.Symbol, error) {
return acc, nil
}

//AcceptInsetPattern in a stream
// AcceptInsetPattern in a stream
func AcceptInsetPattern(stream Stream, left uint32, right uint32, seq ...uint32) ([]text.Symbol, error) {
acc := []text.Symbol{}
matchLeft := Expect(stream, left)
Expand All @@ -111,9 +111,9 @@ func AcceptInsetPattern(stream Stream, left uint32, right uint32, seq ...uint32)
return acc, matchRight
}

//AcceptInsetDelegate read left expected symbol, then test for the right expected symbol.
//If symbol is not the specified symbol this function will delegate the inset matching on the provided function
//In the end the right symbol is read
// AcceptInsetDelegate read left expected symbol, then test for the right expected symbol.
// If symbol is not the specified symbol this function will delegate the inset matching on the provided function
// In the end the right symbol is read
func AcceptInsetDelegate(stream Stream, left uint32, right uint32, dg Match) ([]CSTNode, error) {
leafs := []CSTNode{}
matchLeft := Expect(stream, left)
Expand All @@ -131,7 +131,7 @@ func AcceptInsetDelegate(stream Stream, left uint32, right uint32, dg Match) ([]
return leafs, matchRight
}

//AcceptPatternWithTest in the stream source
// AcceptPatternWithTest in the stream source
func AcceptPatternWithTest(stream Stream, left uint32, right uint32, test uint32, dg Match) ([]CSTNode, error) {
leafs := []CSTNode{}
matchLeft := Expect(stream, left)
Expand Down

0 comments on commit 348a833

Please sign in to comment.