Language
The Language struct represents the current programming language,
defined by a file extension and a set of keywords.
type Language struct {
Extension string `json:"extension"`
Keys []string `json:"keys"`
}
Keyword Loading
The keywords stored in Keys are loaded from application assets,
where the keyword lists for each language are kept.
This allows to preserve the original case of the keywords
This design are good for:
- Updating or expanding keyword lists without recompiling the program.
- Keeping configuration data separate from application logic.
| Field | Type | JSON Tag | Description |
|---|---|---|---|
Extension | string | "extension" | File extension (e.g., "go", "js", "py") that identifies the language or format. |
Keys | []string | "keys" | List of keywords associated with the language, loaded from application assets. |
Language APIs
IsKeyWord
if cc_lib.IsKeyWord(lang, token) {
fmt.Printf("%s", (token))
}
The IsKeyWord function checks whether a given token is one of the keywords defined in a specific Language struct.
It returns true if the token matches any keyword in the language, otherwise it returns false.
This is useful for syntax highlighting, code analysis, or any feature that needs to recognize reserved words.
Parameters
| Name | Type | Description |
|---|---|---|
lang | *Language | Pointer to a Language struct to check against. |
token | string | Word or symbol to verify as a keyword. |
Returns
| Type | Description |
|---|---|
bool | true if token matches a keyword in lang, otherwise false. |
LoadLang
languages_data, err_json := os.ReadFile(`assets/languages.json`)
var langs []cc_lib.Language
err_lang := json.Unmarshal(languages_data, &langs)
if err_lang != nil {
fmt.Println("Problem with reading the file")
}
selectedLanguage := cc_lib.LoadLang(langs, ext)
Parameters
| Name | Type | Description |
|---|---|---|
langs | []Language | Slice of available Language definitions. |
ext | string | File extension to find (e.g., "go", "py"). |
Returns
| Type | Description |
|---|---|
*Language | Pointer to the matching Language struct if found, or nil otherwise. |