Added parsing named types
This commit is contained in:
parent
d693132513
commit
0fef43e13d
@ -62,6 +62,7 @@ func (this *Parser) ParseInto (tree *Tree) error {
|
||||
// expect checks the current token to see if it matches a list of token kind(s),
|
||||
// else it returns an error describing what it expected.
|
||||
func (this *Parser) expect (allowed ...lexer.TokenKind) error {
|
||||
// fmt.Println("expect", this.token, allowed)
|
||||
if !this.token.Is(allowed...) {
|
||||
return errors.Errorf (
|
||||
this.token.Position, "unexpected %v; expected %s",
|
||||
@ -74,6 +75,7 @@ func (this *Parser) expect (allowed ...lexer.TokenKind) error {
|
||||
// manually. This can be helpful when a large syntactical entity is expected and
|
||||
// the first token(s) of it offer insufficient information.
|
||||
func (this *Parser) expectDesc (description string, allowed ...lexer.TokenKind) error {
|
||||
// fmt.Println("expectDesc", this.token, description, allowed)
|
||||
if !this.token.Is(allowed...) {
|
||||
return errors.Errorf (
|
||||
this.token.Position, "unexpected %v; expected %s",
|
||||
@ -86,6 +88,7 @@ func (this *Parser) expectDesc (description string, allowed ...lexer.TokenKind)
|
||||
func (this *Parser) expectNext (allowed ...lexer.TokenKind) error {
|
||||
err := this.next()
|
||||
if err != nil { return err }
|
||||
// fmt.Println("expectNext", this.token, allowed)
|
||||
return this.expect(allowed...)
|
||||
}
|
||||
|
||||
@ -93,12 +96,14 @@ func (this *Parser) expectNext (allowed ...lexer.TokenKind) error {
|
||||
func (this *Parser) expectNextDesc (description string, allowed ...lexer.TokenKind) error {
|
||||
err := this.next()
|
||||
if err != nil { return err }
|
||||
// fmt.Println("expectNextDesc", this.token, description, allowed)
|
||||
return this.expectDesc(description, allowed...)
|
||||
}
|
||||
|
||||
// expectValue returns an error if the current token's value does not match the
|
||||
// allowed values.
|
||||
func (this *Parser) expectValue (kind lexer.TokenKind, allowed ...string) error {
|
||||
// fmt.Println("expectValue", this.token, kind, allowed)
|
||||
if !this.token.Is(kind) || !this.token.ValueIs(allowed...){
|
||||
return errors.Errorf (
|
||||
this.token.Position, "unexpected %v; expected %s",
|
||||
@ -110,6 +115,7 @@ func (this *Parser) expectValue (kind lexer.TokenKind, allowed ...string) error
|
||||
// expectValueDesc is like expectValue, but the expected value(s) are described
|
||||
// manually.
|
||||
func (this *Parser) expectValueDesc (description string, kind lexer.TokenKind, allowed ...string) error {
|
||||
// fmt.Println("expectValueDesc", this.token, description, kind, allowed)
|
||||
if !this.token.Is(kind) || !this.token.ValueIs(allowed...) {
|
||||
return errors.Errorf (
|
||||
this.token.Position, "unexpected %v; expected %s",
|
||||
@ -125,13 +131,18 @@ func (this *Parser) next () error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Parser) bug () string {
|
||||
return fmt.Sprintln("bug detected in the compiler! token:", this.token.Kind)
|
||||
}
|
||||
|
||||
func (this *Parser) parse () error {
|
||||
err := this.next()
|
||||
if err != nil { return err }
|
||||
for {
|
||||
for this.token.Kind != lexer.EOF {
|
||||
err = this.parseTopLevel()
|
||||
if err != nil { return err }
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func commaList[ELEMENT any] (items ...ELEMENT) string {
|
||||
|
@ -1,7 +1,6 @@
|
||||
package parser
|
||||
|
||||
import "git.tebibyte.media/sashakoshka/fspl/lexer"
|
||||
import "git.tebibyte.media/sashakoshka/fspl/errors"
|
||||
import "git.tebibyte.media/sashakoshka/fspl/entity"
|
||||
|
||||
var descriptionTopLevel = "Typedef, Function, or Method"
|
||||
@ -18,7 +17,7 @@ func (this *Parser) parseTopLevel () error {
|
||||
startTokensTopLevel...)
|
||||
if err != nil { return err }
|
||||
if this.token.EOF() { return nil }
|
||||
|
||||
|
||||
access := entity.AccessPrivate
|
||||
if this.token.Kind == lexer.Symbol {
|
||||
access, err = this.parseAccess()
|
||||
@ -60,6 +59,7 @@ func (this *Parser) parseTopLevel () error {
|
||||
typedef, err := this.parseTypedefCore(access, typeName)
|
||||
if err != nil { return err }
|
||||
this.tree.AddDeclaration(typedef)
|
||||
default: this.bug()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -74,9 +74,7 @@ func (this *Parser) parseAccess () (entity.Access, error) {
|
||||
case "-": return entity.AccessPrivate, nil
|
||||
case "~": return entity.AccessRestricted, nil
|
||||
case "+": return entity.AccessPublic, nil
|
||||
default : return 0, errors.Errorf (
|
||||
this.token.Position,
|
||||
"unrecognized access control specifier")
|
||||
default: panic(this.bug())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,76 @@ var startTokensType = []lexer.TokenKind {
|
||||
}
|
||||
|
||||
func (this *Parser) parseType () (entity.Type, error) {
|
||||
println(this.token.String())
|
||||
err := this.expectDesc(descriptionType, startTokensType...)
|
||||
if err != nil { return nil, err }
|
||||
|
||||
switch this.token.Kind {
|
||||
case lexer.Ident:
|
||||
ident := this.token.Value
|
||||
err := this.expectNext(lexer.DoubleColon)
|
||||
if err != nil { return nil, err }
|
||||
err = this.expectNext(lexer.TypeIdent)
|
||||
if err != nil { return nil, err }
|
||||
return this.parseTypeNamedCore(ident)
|
||||
|
||||
case lexer.TypeIdent:
|
||||
return this.parseTypeNamedCore("")
|
||||
|
||||
case lexer.Star:
|
||||
case lexer.Int:
|
||||
case lexer.LParen:
|
||||
}
|
||||
panic(this.bug())
|
||||
}
|
||||
|
||||
func (this *Parser) parseTypeNamedCore (module string) (entity.Type, error) {
|
||||
err := this.expect(lexer.TypeIdent)
|
||||
if err != nil { return nil, err }
|
||||
typeName := this.token.Value
|
||||
this.next()
|
||||
return &entity.TypeNamed {
|
||||
Name: typeName,
|
||||
Module: module,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (this *Parser) parseTypePointer () (entity.Type, error) {
|
||||
// TODO
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (this *Parser) parseTypeSlice () (entity.Type, error) {
|
||||
// TODO
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (this *Parser) parseTypeArray () (entity.Type, error) {
|
||||
// TODO
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (this *Parser) parseTypeStruct () (entity.Type, error) {
|
||||
// TODO
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (this *Parser) parseTypeInterface () (entity.Type, error) {
|
||||
// TODO
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (this *Parser) parseTypeInt () (entity.Type, error) {
|
||||
// TODO
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (this *Parser) parseTypeWord () (entity.Type, error) {
|
||||
// TODO
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (this *Parser) parseTypeFloat () (entity.Type, error) {
|
||||
// TODO
|
||||
return nil, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user