Explorar el Código

Adding...

master
Ognjen Milan Robovic hace 9 meses
padre
commit
e7a62d595b
Se han modificado 5 ficheros con 278 adiciones y 0 borrados
  1. +8
    -0
      example/compile.sh
  2. BIN
      example/example
  3. +84
    -0
      example/example.c
  4. +8
    -0
      install.sh
  5. +178
    -0
      xyntax.h

+ 8
- 0
example/compile.sh Ver fichero

@@ -0,0 +1,8 @@
#!/bin/bash

set -xe

gcc -ansi -Wall -Wextra -Wpedantic -o example example.c
cat example.c | ./example

exit

BIN
example/example Ver fichero


+ 84
- 0
example/example.c Ver fichero

@@ -0,0 +1,84 @@
#define XYNTAX_DECLARATION
#define XYNTAX_DEFINITION

#include <xolatile/xyntax.h>

#define ALLOCATION_CHUNK (1024)

int main (void) {
int buffer_size = 0;
int offset = 0;
int word = 0;
char * buffer = NULL;

char separator [29] = ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n";

char * c_keywords [32] = {
"register", "volatile", "auto", "const", "static", "extern", "if", "else",
"do", "while", "for", "continue", "switch", "case", "default", "break",
"enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
"char", "short", "int", "long", "signed", "unsigned", "float", "double"
};

char * c_preprocessor [8] = {
"#include", "#define", "#ifdef", "#ifndef", "#undef", "#elif", "#if", "#endif"
};

char * c_common [4] = {
"NULL", "FILE", "size_t", "ssize_t"
};

xyntax_define (0, 0, 0, "//", "\n", '\0', XYNTAX_GREY, XYNTAX_BOLD);
xyntax_define (0, 0, 0, "/*", "*/", '\0', XYNTAX_GREY, XYNTAX_BOLD);
xyntax_define (0, 0, 0, "'", "'", '\\', XYNTAX_PINK, XYNTAX_BOLD);
xyntax_define (0, 0, 0, "\"", "\"", '\\', XYNTAX_RED, XYNTAX_BOLD);

do {
xyntax_define (0, 0, 1, c_keywords [word], separator, '\0', XYNTAX_YELLOW, XYNTAX_BOLD);
} while (++word != 32);

word = 0;

do {
xyntax_define (0, 0, 1, c_preprocessor [word], separator, '\0', XYNTAX_YELLOW, XYNTAX_NORMAL);
} while (++word != 8);

word = 0;

do {
xyntax_define (0, 0, 1, c_common [word], separator, '\0', XYNTAX_YELLOW, XYNTAX_NORMAL);
} while (++word != 4);

xyntax_define (0, 1, 0, "()[]{}", "", '\0', XYNTAX_GREEN, XYNTAX_BOLD);
xyntax_define (0, 1, 0, ".,:;<=>+-*/%!&~^", "", '\0', XYNTAX_BLUE, XYNTAX_BOLD);
xyntax_define (0, 1, 1, "0123456789", separator, '\0', XYNTAX_CYAN, XYNTAX_BOLD);
xyntax_define (0, 1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separator, '\0', XYNTAX_PINK, XYNTAX_ITALIC);
xyntax_define (0, 1, 1, "abcdefghijklmnopqrstuvwxyz", separator, '\0', XYNTAX_WHITE, XYNTAX_ITALIC);
xyntax_define (0, 0, 1, "_", separator, '\0', XYNTAX_YELLOW, XYNTAX_ITALIC);

buffer = realloc (buffer, ALLOCATION_CHUNK);

do {
if ((buffer_size + 1) % ALLOCATION_CHUNK == 0) {
buffer = realloc (buffer, (unsigned long int) ((buffer_size + 1) / ALLOCATION_CHUNK + 1) * ALLOCATION_CHUNK);
}

buffer [buffer_size] = '\0';

(void) read (STDIN_FILENO, & buffer [buffer_size], sizeof (* buffer));

++buffer_size;
} while (buffer [buffer_size - 1] != '\0');

buffer [buffer_size - 1] = '\0';

do {
offset += xyntax_output (& buffer [offset]);
} while (buffer [offset] != '\0');

free (buffer);

xyntax_delete ();

return (EXIT_SUCCESS);
}

+ 8
- 0
install.sh Ver fichero

@@ -0,0 +1,8 @@
#!/bin/bash

set -xe

sudo mkdir /usr/include/xolatile
sudo cp xyntax.h /usr/include/xolatile/xyntax.h

exit

+ 178
- 0
xyntax.h Ver fichero

@@ -0,0 +1,178 @@
/*
* Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
*
* Xyntax is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
* And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
* It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3.
*/

#ifdef XYNTAX_DECLARATION
#undef XYNTAX_DECLARATION

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define XYNTAX_NORMAL (0)
#define XYNTAX_BOLD (1)
#define XYNTAX_ITALIC (3)
#define XYNTAX_UNDERLINE (4)
#define XYNTAX_REVERSE (7)

enum {
XYNTAX_GREY,
XYNTAX_RED,
XYNTAX_GREEN,
XYNTAX_YELLOW,
XYNTAX_BLUE,
XYNTAX_PINK,
XYNTAX_CYAN,
XYNTAX_WHITE
};

extern int xyntax_count;
extern int * xyntax_mode;
extern int * xyntax_enrange;
extern int * xyntax_derange;
extern char * * xyntax_begin;
extern char * * xyntax_end;
extern char * xyntax_escape;
extern int * xyntax_colour;
extern int * xyntax_effect;

extern void xyntax_define (int, int, int, char *, char *, char, int, int);
extern int xyntax_output (char *);
extern void xyntax_delete (void);

#endif

#ifdef XYNTAX_DEFINITION
#undef XYNTAX_DEFINITION

int xyntax_count = 0;
int * xyntax_mode = NULL;
int * xyntax_enrange = NULL;
int * xyntax_derange = NULL;
char * * xyntax_begin = NULL;
char * * xyntax_end = NULL;
char * xyntax_escape = NULL;
int * xyntax_colour = NULL;
int * xyntax_effect = NULL;

void xyntax_define (int mode, int enrange, int derange, char * begin, char * end, char escape, int colour, int effect) {
xyntax_mode = realloc (xyntax_mode, (unsigned long int) (xyntax_count + 1) * sizeof (* xyntax_mode));
xyntax_enrange = realloc (xyntax_enrange, (unsigned long int) (xyntax_count + 1) * sizeof (* xyntax_enrange));
xyntax_derange = realloc (xyntax_derange, (unsigned long int) (xyntax_count + 1) * sizeof (* xyntax_derange));
xyntax_begin = realloc (xyntax_begin, (unsigned long int) (xyntax_count + 1) * sizeof (* xyntax_begin));
xyntax_end = realloc (xyntax_end, (unsigned long int) (xyntax_count + 1) * sizeof (* xyntax_end));
xyntax_escape = realloc (xyntax_escape, (unsigned long int) (xyntax_count + 1) * sizeof (* xyntax_escape));
xyntax_colour = realloc (xyntax_colour, (unsigned long int) (xyntax_count + 1) * sizeof (* xyntax_colour));
xyntax_effect = realloc (xyntax_effect, (unsigned long int) (xyntax_count + 1) * sizeof (* xyntax_effect));

xyntax_begin [xyntax_count] = calloc (strlen (begin) + 1UL, sizeof (* * xyntax_begin));
xyntax_end [xyntax_count] = calloc (strlen (end) + 1UL, sizeof (* * xyntax_end));

xyntax_mode [xyntax_count] = mode;
xyntax_enrange [xyntax_count] = enrange;
xyntax_derange [xyntax_count] = derange;
xyntax_escape [xyntax_count] = escape;
xyntax_colour [xyntax_count] = colour;
xyntax_effect [xyntax_count] = effect;

(void) strcpy (xyntax_begin [xyntax_count], begin);
(void) strcpy (xyntax_end [xyntax_count], end);

++xyntax_count;
}

int xyntax_output (char * string) {
int offset = 0;
int select = 0;
char format [8] = "\033[ ;3 m";

do {
if (xyntax_enrange [select] == 0) {
if (strncmp (string, xyntax_begin [select], strlen (xyntax_begin [select])) == 0) {
break;
}
} else {
int subset = 0;
do {
if (string [offset] == xyntax_begin [select] [subset]) {
goto selected;
}
} while (++subset != (int) strlen (xyntax_begin [select]));
}
} while (++select != xyntax_count);

selected:

if (select == xyntax_count) {
format [2] = (char) XYNTAX_NORMAL + '0';
format [5] = (char) XYNTAX_WHITE + '0';
(void) write (STDOUT_FILENO, format, sizeof (format));
(void) write (STDOUT_FILENO, & string [offset], sizeof (string [offset]));
(void) write (STDOUT_FILENO, "\033[0m", sizeof ("\033[0m"));
return (1);
}

format [2] = (char) xyntax_effect [select] + '0';
format [5] = (char) xyntax_colour [select] + '0';

(void) write (STDOUT_FILENO, format, sizeof (format));

do {
(void) write (STDOUT_FILENO, & string [offset], sizeof (string [offset]));

++offset;

if (string [offset] == xyntax_escape [select]) {
(void) write (STDOUT_FILENO, & string [offset], sizeof (string [offset]));
++offset;
continue;
}

if (xyntax_derange [select] == 0) {
if (strncmp (& string [offset], xyntax_end [select], strlen (xyntax_end [select])) == 0) {
(void) write (STDOUT_FILENO, xyntax_end [select], strlen (xyntax_end [select]));
(void) write (STDOUT_FILENO, "\033[0m", sizeof ("\033[0m"));
return (offset + (int) strlen (xyntax_end [select]));
}
} else {
int subset = 0;
if (strcmp (xyntax_end [select], "") == 0) {
break;
} do {
if (string [offset] == xyntax_end [select] [subset]) {
goto finished;
}
} while (++subset != (int) strlen (xyntax_end [select]));
}
} while (string [offset] != '\0');

finished:

(void) write (STDOUT_FILENO, "\033[0m", sizeof ("\033[0m"));

return (offset);
}

void xyntax_delete (void) {
--xyntax_count;

do {
free (xyntax_begin [xyntax_count]);
free (xyntax_end [xyntax_count]);
} while (--xyntax_count != -1);

free (xyntax_mode);
free (xyntax_enrange);
free (xyntax_derange);
free (xyntax_begin);
free (xyntax_end);
free (xyntax_escape);
free (xyntax_colour);
free (xyntax_effect);
}

#endif

Cargando…
Cancelar
Guardar