xolatilization/xanguage.h

232 lines
11 KiB
C

/// __ ____ _ _ __ __ _ _ _ __ _ __ _ ___
/// \ \/ / _` | '_ \ / _` | | | |/ _` |/ _` |/ _ \
/// > < (_| | | | | (_| | |_| | (_| | (_| | __/
/// /_/\_\__,_|_| |_|\__, |\__,_|\__,_|\__, |\___|
/// |___/ |___/
///
/// Copyright (c) 1997 - Ognjen 'xolatile' Milan Robovic
///
/// xolatile@chud.cyou - xanguage - Syntax definitions of programming languages that I care about (and some that I don't care about).
///
/// This program is free software, free as in freedom and as in free beer, you can redistribute it and/or modify it under the terms of the GNU
/// General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version if you wish...
///
/// This program is distributed in the hope that it will be useful, but it is probably not, and without any warranty, without even the implied
/// warranty of merchantability or fitness for a particular purpose, because it is pointless. Please see the GNU (Geenoo) General Public License
/// for more details, if you dare, it is a lot of text that nobody wants to read...
/// Description
///
/// Xanguage, core programming and scripting language definitions created using Xyntax, with few more output definitions that I care about, for
/// example Valgrind. There's not much to document about this library, it's used in my projects Xarbon and Xighlight. You can add support for
/// your language of choice by adding one enumeration value, creating header file similar to existing ones, including it here and editing
/// functions below to match 'language_count'. If you don't like how I highlight one or more of these languages, feel free to add more fields in
/// 'language_structure' or edit existing ones inside 'xanguage' folder. This header file may look unfamiliar, especially to my other projects,
/// but that's the cost of highlighting multiple languages in very low amount of code.
/// Enumeration of supported languages, in no particular order.
typedef enum {
language_common, language_ada, language_c, language_cpp,
language_d, language_eaxhla, language_flat, language_fortran,
language_pascal, language_python, language_go, language_lua,
language_bash, language_haskell, language_valgrind, language_holy_c,
language_count
} language_enumeration;
/// Structure of single language definition, highlighting-wise, this will be initialized below, check out how it is used.
///
/// language_structure * language = null;
typedef struct {
natural comment_colour; /// Colours for basic language syntax information.
natural processor_colour;
natural character_colour;
natural string_colour;
natural keyword_colour;
natural type_colour;
natural bracket_colour;
natural operator_colour;
natural number_colour;
natural lowercase_colour;
natural uppercase_colour;
natural underscore_colour;
natural register_colour;
natural extension_colour;
natural fatal_colour;
natural comment_effect; /// Effects for basic language syntax information.
natural processor_effect;
natural character_effect;
natural string_effect;
natural keyword_effect;
natural type_effect;
natural bracket_effect;
natural operator_effect;
natural number_effect;
natural lowercase_effect;
natural uppercase_effect;
natural underscore_effect;
natural register_effect;
natural extension_effect;
natural fatal_effect;
} language_structure;
/// Warning: Local macros! You don't need to use this at all, it's covered by other functions, don't worry about it.
///
/// You shouldn't care about these local macros, they made few lines of code shorter, we're including files in this order, in this place.
#define language_lowercase "abcdefghijklmnopqrstuvwxyz"
#define language_uppercase "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define language_letters "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define language_digits "0123456789"
#include <xolatile/xanguage/common.h>
#include <xolatile/xanguage/ada.h>
#include <xolatile/xanguage/c.h>
#include <xolatile/xanguage/c++.h>
#include <xolatile/xanguage/d.h>
#include <xolatile/xanguage/eaxhla.h>
#include <xolatile/xanguage/flat.h>
#include <xolatile/xanguage/fortran.h>
#include <xolatile/xanguage/pascal.h>
#include <xolatile/xanguage/python.h>
#include <xolatile/xanguage/go.h>
#include <xolatile/xanguage/holy_c.h>
#include <xolatile/xanguage/lua.h>
#include <xolatile/xanguage/bash.h>
#include <xolatile/xanguage/haskell.h>
#include <xolatile/xanguage/valgrind.h>
#undef language_lowercase
#undef language_uppercase
#undef language_letters
#undef language_digits
/// Array of function pointers enumerated above, these functions are defined in separate header files in 'xanguage' folder.
static procedure (* language_highlighter [language_count]) (language_structure * language, syntax_structure * syntax) = {
language_highlight_common, language_highlight_ada, language_highlight_c, language_highlight_cpp,
language_highlight_d, language_highlight_eaxhla, language_highlight_flat, language_highlight_fortran,
language_highlight_pascal, language_highlight_python, language_highlight_go, language_highlight_lua,
language_highlight_bash, language_highlight_haskell, language_highlight_valgrind, language_highlight_holy_c
};
/// Array of short command line argument enumerated above, makes main function less bloated.
static character * language_short_option [language_count] = {
"-X", "-A", "-C", "-S", "-D", "-E", "-T", "-F", "-P", "-Y", "-G", "-L", "-B", "-H", "-V", "-O"
};
/// Array of long command line argument enumerated above, makes main function less bloated.
static character * language_long_option [language_count] = {
"--common", "--ada", "--c", "--cpp", "--d", "--eaxhla", "--flat", "--fortran",
"--pascal", "--python", "--go", "--lua", "--bash", "--haskell", "--valgrind", "--holyc"
};
/// Array of pretty language name enumerated above, makes things pretty.
static character * language_identifier [language_count] = {
"Common", "Ada", "C", "C++", "D", "EAXHLA", "Flat", "Fortran",
"Pascal", "Python", "Go", "Lua", "Bash", "Haskell", "Valgrind", "Holy C"
};
/// After you've defined global or local language variable, you want to initialize it, edit this function if you dislike my themes.
///
/// language = language_initialize (false); /// You're probably using this library to render an image.
/// language = language_initialize (true); /// You're probably using this library to print to terminal.
static language_structure * language_initialize (boolean true_colour) {
language_structure * language = allocate (sizeof (* language));
if (true_colour == true) {
language->comment_colour = 0xff777777u;
language->processor_colour = 0xff3377aau;
language->character_colour = 0xff7733ccu;
language->string_colour = 0xffcc3377u;
language->keyword_colour = 0xff33cceeu;
language->type_colour = 0xff55cceeu;
language->bracket_colour = 0xffee5533u;
language->operator_colour = 0xffeeaa33u;
language->number_colour = 0xffee33aau;
language->lowercase_colour = 0xffccccccu;
language->uppercase_colour = 0xffeeeeeeu;
language->underscore_colour = 0xffaaaaaau;
language->register_colour = 0xff5577aau;
language->extension_colour = 0xff55aaccu;
language->fatal_colour = 0xffcc7755u;
} else {
language->comment_colour = colour_grey;
language->processor_colour = colour_cyan;
language->character_colour = colour_pink;
language->string_colour = colour_pink;
language->keyword_colour = colour_yellow;
language->type_colour = colour_yellow;
language->bracket_colour = colour_blue;
language->operator_colour = colour_cyan;
language->number_colour = colour_pink;
language->lowercase_colour = colour_white;
language->uppercase_colour = colour_white;
language->underscore_colour = colour_white;
language->register_colour = colour_cyan;
language->extension_colour = colour_yellow;
language->fatal_colour = colour_red;
language->comment_effect = effect_bold;
language->processor_effect = effect_italic;
language->character_effect = effect_bold;
language->string_effect = effect_normal;
language->keyword_effect = effect_bold;
language->type_effect = effect_normal;
language->bracket_effect = effect_bold;
language->operator_effect = effect_normal;
language->number_effect = effect_bold;
language->lowercase_effect = effect_normal;
language->uppercase_effect = effect_bold;
language->underscore_effect = effect_italic;
language->register_effect = effect_italic;
language->extension_effect = effect_italic;
language->fatal_effect = effect_bold;
}
return (language);
}
/// When you're done doing your thing, just call this function nicely to avoid leaking memory.
///
/// language = language_deinitialize (language);
static language_structure * language_deinitialize (language_structure * language) {
return (deallocate (language));
}
/// This is just a helper function that I used a lot, it made sense to define it in this file, so here it is... This function will call specific
/// function from 'xanguage' folder, based on what file type you pass to it, enumerated in Xtandard. It takes natural number as selection value
/// due to avoiding type-related errors that dumb compilers might warn about, because some C compilers treat enumeration values as signed, while
/// others treat them as unsigned, unless you manually define some value there negative, which is braindead.
///
/// language_conditionally_select (language, syntax, selection_index);
static procedure language_conditionally_select (language_structure * language, syntax_structure * syntax, natural select) {
if (syntax->count == 0) {
if ((select == file_type_c_source) || (select == file_type_c_header)) {
language_highlight_c (language, syntax);
} else if ((select == file_type_ada_sexy_body) || (select == file_type_ada_specification)) {
language_highlight_ada (language, syntax);
} else if ((select == file_type_cpp_source) || (select == file_type_cpp_header)) {
language_highlight_cpp (language, syntax);
} else if (select == file_type_flat_assembly) {
language_highlight_flat (language, syntax);
} else if (select == file_type_fortran_90_source) {
language_highlight_fortran (language, syntax);
} else if (select == file_type_pascal_source) {
language_highlight_pascal (language, syntax);
} else if (select == file_type_eax_assembly) {
language_highlight_eaxhla (language, syntax);
} else if (select == file_type_python_script) {
language_highlight_python (language, syntax);
} else {
language_highlight_common (language, syntax);
}
}
}