本文整理汇总了Golang中github.com/ark-lang/ark/src/lexer.NewSpan函数的典型用法代码示例。如果您正苦于以下问题:Golang NewSpan函数的具体用法?Golang NewSpan怎么用?Golang NewSpan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewSpan函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseIfStat
func (v *parser) parseIfStat() *IfStatNode {
defer un(trace(v, "ifstat"))
if !v.tokenMatches(0, lexer.TOKEN_IDENTIFIER, KEYWORD_IF) {
return nil
}
startToken := v.consumeToken()
var parts []*ConditionBodyNode
var lastPart *ConditionBodyNode
for {
condition := v.parseExpr()
if condition == nil {
v.err("Expected valid expression as condition in if statement")
}
body := v.parseBlock()
if body == nil {
v.err("Expected valid block after condition in if statement")
}
lastPart = &ConditionBodyNode{Condition: condition, Body: body}
lastPart.SetWhere(lexer.NewSpan(condition.Where().Start(), body.Where().End()))
parts = append(parts, lastPart)
if !v.tokensMatch(lexer.TOKEN_IDENTIFIER, KEYWORD_ELSE, lexer.TOKEN_IDENTIFIER, KEYWORD_IF) {
break
}
v.consumeTokens(2)
}
var elseBody *BlockNode
if v.tokenMatches(0, lexer.TOKEN_IDENTIFIER, KEYWORD_ELSE) {
v.consumeToken()
elseBody = v.parseBlock()
if elseBody == nil {
v.err("Expected valid block after `else` keyword in if statement")
}
}
res := &IfStatNode{Parts: parts, ElseBody: elseBody}
if elseBody != nil {
res.SetWhere(lexer.NewSpan(startToken.Where.Start(), elseBody.Where().End()))
} else {
res.SetWhere(lexer.NewSpan(startToken.Where.Start(), lastPart.Where().End()))
}
return res
}
示例2: parseName
func (v *parser) parseName() *NameNode {
if !v.nextIs(lexer.TOKEN_IDENTIFIER) {
return nil
}
var parts []LocatedString
for {
if !v.nextIs(lexer.TOKEN_IDENTIFIER) {
v.err("Expected identifier after `::` in name, got `%s`", v.peek(0).Contents)
}
part := NewLocatedString(v.consumeToken())
if isReservedKeyword(part.Value) {
v.err("Cannot use reserved keyword `%s` as name", part.Value)
}
parts = append(parts, part)
if !v.tokenMatches(0, lexer.TOKEN_OPERATOR, "::") {
break
}
v.consumeToken()
}
name, parts := parts[len(parts)-1], parts[:len(parts)-1]
res := &NameNode{Modules: parts, Name: name}
if len(parts) > 0 {
res.SetWhere(lexer.NewSpan(parts[0].Where.Start(), name.Where.End()))
} else {
res.SetWhere(name.Where)
}
return res
}
示例3: parseMatchStat
func (v *parser) parseMatchStat() *MatchStatNode {
defer un(trace(v, "matchstat"))
if !v.tokenMatches(0, lexer.TOKEN_IDENTIFIER, KEYWORD_MATCH) {
return nil
}
startToken := v.consumeToken()
value := v.parseExpr()
if value == nil {
v.err("Expected valid expresson as value in match statement")
}
v.expect(lexer.TOKEN_SEPARATOR, "{")
var cases []*MatchCaseNode
for {
if v.tokenMatches(0, lexer.TOKEN_SEPARATOR, "}") {
break
}
var pattern ParseNode
if v.tokenMatches(0, lexer.TOKEN_IDENTIFIER, "_") {
patTok := v.consumeToken()
pattern = &DefaultPatternNode{}
pattern.SetWhere(patTok.Where)
} else {
pattern = v.parseExpr()
}
if pattern == nil {
v.err("Expected valid expression as pattern in match statement")
}
v.expect(lexer.TOKEN_OPERATOR, "=>")
var body ParseNode
if v.tokenMatches(0, lexer.TOKEN_SEPARATOR, "{") {
body = v.parseBlock()
} else {
body = v.parseStat()
}
if body == nil {
v.err("Expected valid arm statement in match clause")
}
v.expect(lexer.TOKEN_SEPARATOR, ",")
caseNode := &MatchCaseNode{Pattern: pattern, Body: body}
caseNode.SetWhere(lexer.NewSpan(pattern.Where().Start(), body.Where().End()))
cases = append(cases, caseNode)
}
endToken := v.expect(lexer.TOKEN_SEPARATOR, "}")
res := &MatchStatNode{Value: value, Cases: cases}
res.SetWhere(lexer.NewSpanFromTokens(startToken, endToken))
return res
}
示例4: parseArrayType
func (v *parser) parseArrayType() *ArrayTypeNode {
startToken := v.consumeToken()
length := v.parseNumberLit()
if length != nil && length.IsFloat {
v.err("Expected integer length for array type")
}
if !v.tokenMatches(0, lexer.TOKEN_SEPARATOR, "]") {
v.err("Expected closing `]` in array type, got `%s`", v.peek(0).Contents)
}
v.consumeToken()
memberType := v.parseType()
if memberType == nil {
v.err("Expected valid type in array type")
}
res := &ArrayTypeNode{MemberType: memberType}
if length != nil {
// TODO: Defend against overflow
res.Length = int(length.IntValue)
}
res.SetWhere(lexer.NewSpan(startToken.Where.Start(), memberType.Where().End()))
return res
}
示例5: parseEnumEntry
func (v *parser) parseEnumEntry() *EnumEntryNode {
if !v.nextIs(lexer.TOKEN_IDENTIFIER) {
return nil
}
name := v.consumeToken()
if isReservedKeyword(name.Contents) {
v.err("Cannot use reserved keyword `%s` as name for enum entry", name.Contents)
}
var value ParseNode
if v.tokenMatches(0, lexer.TOKEN_OPERATOR, "=") {
v.consumeToken()
value = v.parseExpr()
if value == nil {
v.err("Expected valid expression after `=` in enum entry")
}
}
res := &EnumEntryNode{Name: NewLocatedString(name), Value: value}
if value != nil {
res.SetWhere(lexer.NewSpan(name.Where.Start(), value.Where().End()))
} else {
res.SetWhere(name.Where)
}
return res
}
示例6: parseAssignStat
func (v *parser) parseAssignStat() ParseNode {
startPos := v.currentToken
accessExpr := v.parseAccessExpr()
if accessExpr == nil || !v.tokenMatches(0, lexer.TOKEN_OPERATOR, "=") {
v.currentToken = startPos
return nil
}
// consume '='
v.consumeToken()
value := v.parseExpr()
if value == nil {
v.err("Expected valid expression in assignment statement")
}
if !v.tokenMatches(0, lexer.TOKEN_SEPARATOR, ";") {
v.err("Expected `;` after assignment statement, got `%s`", v.peek(0).Contents)
}
endToken := v.consumeToken()
res := &AssignStatNode{Target: accessExpr, Value: value}
res.SetWhere(lexer.NewSpan(accessExpr.Where().Start(), endToken.Where.End()))
return res
}
示例7: parseToplevelDirective
func (v *parser) parseToplevelDirective() ParseNode {
defer un(trace(v, "toplevel-directive"))
if !v.tokensMatch(lexer.TOKEN_OPERATOR, "#", lexer.TOKEN_IDENTIFIER, "") {
return nil
}
start := v.expect(lexer.TOKEN_OPERATOR, "#")
directive := v.expect(lexer.TOKEN_IDENTIFIER, "")
switch directive.Contents {
case "link":
library := v.expect(lexer.TOKEN_STRING, "")
res := &LinkDirectiveNode{Library: NewLocatedString(library)}
res.SetWhere(lexer.NewSpanFromTokens(start, library))
return res
case "use":
module := v.parseName()
if module == nil {
v.errPosSpecific(directive.Where.End(), "Expected name after use directive")
}
v.deps = append(v.deps, module)
res := &UseDirectiveNode{Module: module}
res.SetWhere(lexer.NewSpan(start.Where.Start(), module.Where().End()))
return res
default:
v.errTokenSpecific(directive, "No such directive `%s`", directive.Contents)
return nil
}
}
示例8: parseBinopAssignStat
func (v *parser) parseBinopAssignStat() ParseNode {
defer un(trace(v, "binopassignstat"))
startPos := v.currentToken
accessExpr := v.parseExpr()
if accessExpr == nil || !v.tokensMatch(lexer.TOKEN_OPERATOR, "", lexer.TOKEN_OPERATOR, "=") {
v.currentToken = startPos
return nil
}
typ := stringToBinOpType(v.peek(0).Contents)
if typ == BINOP_ERR || typ.Category() == OP_COMPARISON {
v.err("Invalid binary operator `%s`", v.peek(0).Contents)
}
v.consumeToken()
// consume '='
v.consumeToken()
var value ParseNode
value = v.parseCompositeLiteral()
if value == nil {
value = v.parseExpr()
}
// no composite and no expr = err
if value == nil {
v.err("Expected valid expression in assignment statement")
}
res := &BinopAssignStatNode{Target: accessExpr, Operator: typ, Value: value}
res.SetWhere(lexer.NewSpan(accessExpr.Where().Start(), value.Where().End()))
return res
}
示例9: parseArrayType
func (v *parser) parseArrayType() *ArrayTypeNode {
defer un(trace(v, "arraytype"))
if !v.tokenMatches(0, lexer.TOKEN_SEPARATOR, "[") {
return nil
}
startToken := v.consumeToken()
length := v.parseNumberLit()
if length != nil && length.IsFloat {
v.err("Expected integer length for array type")
}
v.expect(lexer.TOKEN_SEPARATOR, "]")
memberType := v.parseType(true)
if memberType == nil {
v.err("Expected valid type in array type")
}
res := &ArrayTypeNode{MemberType: memberType}
if length != nil {
// TODO: Defend against overflow
res.Length = int(length.IntValue.Int64())
}
res.SetWhere(lexer.NewSpan(startToken.Where.Start(), memberType.Where().End()))
return res
}
示例10: parseArrayLenExpr
func (v *parser) parseArrayLenExpr() *ArrayLenExprNode {
defer un(trace(v, "arraylenexpr"))
if !v.tokenMatches(0, lexer.TOKEN_IDENTIFIER, KEYWORD_LEN) {
return nil
}
startToken := v.consumeToken()
v.expect(lexer.TOKEN_SEPARATOR, "(")
var array ParseNode
array = v.parseCompositeLiteral()
if array == nil {
array = v.parseExpr()
}
if array == nil {
v.err("Expected valid expression in array length expression")
}
v.expect(lexer.TOKEN_SEPARATOR, ")")
end := v.peek(0)
res := &ArrayLenExprNode{ArrayExpr: array}
res.SetWhere(lexer.NewSpan(startToken.Where.Start(), end.Where.Start()))
return res
}
示例11: parseTypeDecl
func (v *parser) parseTypeDecl() *TypeDeclNode {
defer un(trace(v, "typdecl"))
if !v.tokenMatches(0, lexer.TOKEN_IDENTIFIER, "type") {
return nil
}
startToken := v.consumeToken()
name := v.expect(lexer.TOKEN_IDENTIFIER, "")
if isReservedKeyword(name.Contents) {
v.err("Cannot use reserved keyword `%s` as type name", name.Contents)
}
genericSigil := v.parseGenericSigil()
typ := v.parseType(true)
endToken := v.expect(lexer.TOKEN_SEPARATOR, ";")
res := &TypeDeclNode{
Name: NewLocatedString(name),
GenericSigil: genericSigil,
Type: typ,
}
res.SetWhere(lexer.NewSpan(startToken.Where.Start(), endToken.Where.End()))
return res
}
示例12: parseAssignStat
func (v *parser) parseAssignStat() ParseNode {
defer un(trace(v, "assignstat"))
startPos := v.currentToken
accessExpr := v.parseExpr()
if accessExpr == nil || !v.tokenMatches(0, lexer.TOKEN_OPERATOR, "=") {
v.currentToken = startPos
return nil
}
// consume '='
v.consumeToken()
var value ParseNode
value = v.parseCompositeLiteral()
if value == nil {
value = v.parseExpr()
}
// not a composite or expr = error
if value == nil {
v.err("Expected valid expression in assignment statement")
}
res := &AssignStatNode{Target: accessExpr, Value: value}
res.SetWhere(lexer.NewSpan(accessExpr.Where().Start(), value.Where().End()))
return res
}
示例13: parseStringLit
func (v *parser) parseStringLit() *StringLitNode {
defer un(trace(v, "stringlit"))
var cstring bool
var firstToken, stringToken *lexer.Token
if v.tokenMatches(0, lexer.TOKEN_STRING, "") {
cstring = false
firstToken = v.consumeToken()
stringToken = firstToken
} else if v.tokensMatch(lexer.TOKEN_IDENTIFIER, "c", lexer.TOKEN_STRING, "") {
cstring = true
firstToken = v.consumeToken()
stringToken = v.consumeToken()
} else {
return nil
}
unescaped, err := UnescapeString(stringToken.Contents)
if err != nil {
v.errTokenSpecific(stringToken, "Invalid string literal: %s", err)
}
res := &StringLitNode{Value: unescaped, IsCString: cstring}
res.SetWhere(lexer.NewSpan(firstToken.Where.Start(), stringToken.Where.End()))
return res
}
示例14: parseTypeParameter
func (v *parser) parseTypeParameter() *TypeParameterNode {
name := v.expect(lexer.TOKEN_IDENTIFIER, "")
var restrictions []*NameNode
if v.tokenMatches(0, lexer.TOKEN_OPERATOR, ":") {
v.consumeToken()
for {
restriction := v.parseName()
if restriction == nil {
v.err("Expected valid name in type restriction")
}
restrictions = append(restrictions, restriction)
if !v.tokenMatches(0, lexer.TOKEN_OPERATOR, "&") {
break
}
v.consumeToken()
}
}
res := &TypeParameterNode{Name: NewLocatedString(name), Restrictions: restrictions}
if idx := len(restrictions) - 1; idx >= 0 {
res.SetWhere(lexer.NewSpan(name.Where.Start(), restrictions[idx].Where().End()))
} else {
res.SetWhere(lexer.NewSpanFromTokens(name, name))
}
return res
}
示例15: parseName
func (v *parser) parseName() *NameNode {
defer un(trace(v, "name"))
if !v.nextIs(lexer.TOKEN_IDENTIFIER) {
return nil
}
var parts []LocatedString
for {
part := v.expect(lexer.TOKEN_IDENTIFIER, "")
parts = append(parts, NewLocatedString(part))
if !v.tokenMatches(0, lexer.TOKEN_OPERATOR, "::") {
break
}
v.consumeToken()
}
name, parts := parts[len(parts)-1], parts[:len(parts)-1]
res := &NameNode{Modules: parts, Name: name}
if len(parts) > 0 {
res.SetWhere(lexer.NewSpan(parts[0].Where.Start(), name.Where.End()))
} else {
res.SetWhere(name.Where)
}
return res
}