コード例 #1
0
ファイル: token.go プロジェクト: ole108/diamond-lang
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}
}
コード例 #2
0
ファイル: lexfuncs.go プロジェクト: ole108/diamond-lang
func scanSpecialCall(fullId common.SrcPiece) (id string, halfApplied bool) {
	buf := strings.Bytes(fullId.Content())
	i := 0
	if buf[i] == '\\' {
		halfApplied = true
		i++
	}
	id = string(buf[i:len(buf)])
	return
}
コード例 #3
0
ファイル: lexfuncs.go プロジェクト: ole108/diamond-lang
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
}
コード例 #4
0
ファイル: lexfuncs.go プロジェクト: ole108/diamond-lang
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
}
コード例 #5
0
ファイル: lexfuncs.go プロジェクト: ole108/diamond-lang
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
}
コード例 #6
0
ファイル: srcbuf_test.go プロジェクト: ole108/diamond-lang
func testSrcPiece(piece common.SrcPiece, line int, column int,
	content string, wholeLine string, t *testing.T, num int) {
	failed := false
	if piece.StartLine() != line {
		failed = true
		t.Errorf("(%d) Expected line %d but got %d.", num, line, piece.StartLine())
	}
	if piece.StartColumn() != column {
		failed = true
		t.Errorf("(%d) Expected column %d but got %d.", num, column, piece.StartColumn())
	}
	if piece.Content() != content {
		failed = true
		t.Errorf("(%d) Expected content `%v` but got `%v`.", num, content, piece.Content())
	}
	if piece.WholeLine() != wholeLine {
		failed = true
		t.Errorf("(%d) Expected wholeLine `%v` but got `%v`.", num, wholeLine, piece.WholeLine())
	}
	if failed {
		t.FailNow()
	}
}