Skip to main content

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.
FieldTypeJSON TagDescription
Extensionstring"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

NameTypeDescription
lang*LanguagePointer to a Language struct to check against.
tokenstringWord or symbol to verify as a keyword.

Returns

TypeDescription
booltrue 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

NameTypeDescription
langs[]LanguageSlice of available Language definitions.
extstringFile extension to find (e.g., "go", "py").

Returns

TypeDescription
*LanguagePointer to the matching Language struct if found, or nil otherwise.