Exemple #1
0
func (lx *Lexer) newIdTok(typ common.TokEnum, piece common.SrcPiece,
	parts []*IdPart, halfApplied bool) *IdTok {
	if len(parts) <= 0 {
		piece.Error("ID has no parts")
	}
	return &IdTok{&SimpleToken{typ, piece}, parts, halfApplied}
}
Exemple #2
0
func fullId2parts(id string, fullId common.SrcPiece) []*IdPart {
	strParts := strings.Split(id, ".", 0)
	idParts := make([]*IdPart, len(strParts))
	for i, s := range strParts {
		if len(s) <= 0 {
			fullId.Error("Illegal identifier")
		}
		idParts[i] = newIdPart(common.TOK_MODULE_ID, s, s[0] == '_')
	}
	return idParts
}
Exemple #3
0
func setIdTypes(parts []*IdPart, piece common.SrcPiece) common.TokEnum {
	for _, part := range parts {
		part.typ = getIdType(part.id, piece)
	}

	var tokTyp common.TokEnum = common.TOK_MODULE_ID
	for i, part := range parts {
		switch {
		case part.typ == common.TOK_CONST_ID:
			if i <= 1 && tokTyp == common.TOK_MODULE_ID {
				tokTyp = common.TOK_CONST_ID
			} else {
				piece.Error("Illegal constant identifier part")
			}
		case tokTyp == common.TOK_CONST_ID:
			if part.typ != common.TOK_MODULE_ID && part.typ != common.TOK_VAL_ID {
				piece.Error("Illegal constant identifier part")
			}
			part.typ = common.TOK_VAL_ID
		case part.typ == common.TOK_MODULE_ID:
			if tokTyp == common.TOK_FUNC_ID {
				piece.Error("Illegal value after function identifier part")
			}
			if i >= 1 {
				part.typ = common.TOK_VAL_ID
				tokTyp = common.TOK_VAL_ID
			}
		case part.typ == common.TOK_FUNC_ID:
			if i > 1 || tokTyp != common.TOK_MODULE_ID {
				piece.Error("Illegal function identifier")
			}
			tokTyp = common.TOK_FUNC_ID
		case part.typ == common.TOK_VAL_ID:
			if tokTyp == common.TOK_FUNC_ID {
				piece.Error("Illegal value after function identifier part")
			}
			if tokTyp == common.TOK_MODULE_ID {
				tokTyp = common.TOK_VAL_ID
			}
		default:
			piece.Error("Illegal identifier part")
		}
	}

	return tokTyp
}
Exemple #4
0
func getIdType(id string, piece common.SrcPiece) common.TokEnum {
	// ordinary flags:
	gotUpper := false
	gotLower := false
	gotUnder := false
	got2Uppr := false

	// first (and sometimes second) character are special:
	i := 0
	firstUnder := (id[i] == '_')
	if firstUnder {
		i++
	}
	firstUpper := isUpper(id[i])
	firstLower := isLower(id[i])
	i++

	if !firstUpper && !firstLower {
		piece.Error("Illegal start of identifier part")
	}

	// set flags:
	lastUpper := firstUpper
	for ; i < len(id); i++ {
		b := id[i] // SrcBuf guaranties 7 bit clean!
		switch {
		case isUpper(b):
			gotUpper = true
			if lastUpper {
				got2Uppr = true
			}
			lastUpper = true
		case isLower(b):
			gotLower = true
			lastUpper = false
		case isDigit(b):
			// digits are allowed in any ID
			lastUpper = false
		case b == '_':
			gotUnder = true
			lastUpper = false
		default:
			piece.Error("Illegal character in identifier part")
		}
	}

	// evaluate flags:
	var typ common.TokEnum
	switch {
	case !gotUpper && firstLower && !gotUnder && !firstUnder:
		typ = common.TOK_MODULE_ID
	case firstUpper && !gotLower && !firstUnder:
		typ = common.TOK_CONST_ID
	case firstLower && !gotUnder && !got2Uppr:
		typ = common.TOK_VAL_ID
	case firstUpper && gotLower && !gotUnder && !got2Uppr:
		typ = common.TOK_FUNC_ID
	default:
		//  fmt.Println("firstUpper:", firstUpper, ", gotLower:", gotLower, ", gotUnder:", gotUnder, ", got2Uppr:", got2Uppr);
		piece.Error("Illegal identifier part")
	}

	return typ
}