From 20ac640a6d0b8eb6343709ae6ef921b41a31a726 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sun, 1 Jan 2023 23:47:52 -0500 Subject: restructure syntax.c --- Makefile | 2 +- buffer.c | 11 ++++++----- development.md | 8 ++++++++ lsp-json.c | 2 +- syntax.c | 36 ++++++++++++++++++------------------ ted.h | 9 +++++++++ text.c | 2 +- text.h | 2 +- 8 files changed, 45 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index eb73aa5..ee33799 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ INSTALL_BIN_DIR=/usr/bin ted: *.[ch] libpcre2-32.a stb_truetype.o stb_image.o $(CC) main.c stb_truetype.o stb_image.o -o ted $(DEBUG_CFLAGS) $(LIBS) %.o: %.c - $(CC) -Wall $< -c -o $@ + $(CC) -O3 -Wall $< -c -o $@ release: *.[ch] libpcre2-32.a $(CC) main.c -o ted $(RELEASE_CFLAGS) $(LIBS) profile: *.[ch] libpcre2-32.a diff --git a/buffer.c b/buffer.c index 2b5145c..e1c44e9 100644 --- a/buffer.c +++ b/buffer.c @@ -142,7 +142,7 @@ static void buffer_validate_cursor(TextBuffer *buffer) { } // ensure *line points to a line in buffer. -static inline void buffer_validate_line(TextBuffer *buffer, u32 *line) { +static void buffer_validate_line(TextBuffer *buffer, u32 *line) { if (*line >= buffer->nlines) *line = buffer->nlines - 1; } @@ -228,7 +228,7 @@ BufferPos buffer_pos_end_of_file(TextBuffer *buffer) { } // Get the font used for this buffer. -static inline Font *buffer_font(TextBuffer *buffer) { +static Font *buffer_font(TextBuffer *buffer) { return buffer->ted->font; } @@ -1241,7 +1241,7 @@ i64 buffer_pos_move_words(TextBuffer *buffer, BufferPos *pos, i64 nwords) { for (i64 i = 0; i < nwords; ++i) { // move forward one word `nwords` times Line *line = &buffer->lines[pos->line]; u32 index = pos->index; - char32_t const *str = line->str; + const char32_t *str = line->str; if (index == line->len) { if (pos->line >= buffer->nlines - 1) { // end of file reached @@ -1271,7 +1271,7 @@ i64 buffer_pos_move_words(TextBuffer *buffer, BufferPos *pos, i64 nwords) { for (i64 i = 0; i < nwords; ++i) { Line *line = &buffer->lines[pos->line]; u32 index = pos->index; - char32_t const *str = line->str; + const char32_t *str = line->str; if (index == 0) { if (pos->line == 0) { // start of file reached @@ -2737,7 +2737,8 @@ void buffer_render(TextBuffer *buffer, Rect r) { char32_t c = line->str[i]; if (syntax_highlighting) { SyntaxCharType type = char_types[i]; - rgba_u32_to_floats(colors[syntax_char_type_to_color(type)], text_state.color); + ColorSetting color = syntax_char_type_to_color_setting(type); + rgba_u32_to_floats(colors[color], text_state.color); } switch (c) { case '\n': assert(0); break; diff --git a/development.md b/development.md index 19326ef..4f59f1f 100644 --- a/development.md +++ b/development.md @@ -7,10 +7,18 @@ in `util.c` even if they use OS-specific library functions.) ## Header files +## Unicode + +- UTF-32 +- codepoints are characters +- stuff outside BMP on windows + ## Drawing ## Adding settings +## Adding commands + ## Adding languages ## Releasing diff --git a/lsp-json.c b/lsp-json.c index 6d6765d..1e77812 100644 --- a/lsp-json.c +++ b/lsp-json.c @@ -89,7 +89,7 @@ void json_debug_print_value(const JSON *json, JSONValue value); // defining this instead of using isspace seems to be faster // probably because isspace depends on the locale. -static inline bool json_is_space(char c) { +static bool json_is_space(char c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t'; } diff --git a/syntax.c b/syntax.c index 5f957ea..3d8c34f 100644 --- a/syntax.c +++ b/syntax.c @@ -1,3 +1,4 @@ +#include "ted.h" #include "keywords.h" // all characters that can appear in a number @@ -89,8 +90,7 @@ const char *language_comment_end(Language l) { } } -// NOTE: returns the color setting, not the color -ColorSetting syntax_char_type_to_color(SyntaxCharType t) { +ColorSetting syntax_char_type_to_color_setting(SyntaxCharType t) { switch (t) { case SYNTAX_NORMAL: return COLOR_TEXT; case SYNTAX_KEYWORD: return COLOR_KEYWORD; @@ -104,10 +104,10 @@ ColorSetting syntax_char_type_to_color(SyntaxCharType t) { return COLOR_TEXT; } -static inline bool syntax_keyword_matches(char32_t const *text, size_t len, const char *keyword) { +static bool syntax_keyword_matches(const char32_t *text, size_t len, const char *keyword) { if (len == strlen(keyword)) { bool matches = true; - char32_t const *p = text; + const char32_t *p = text; // check if `p` starts with `keyword` for (const char *q = keyword; *q; ++p, ++q) { if (*p != (char32_t)*q) { @@ -148,7 +148,7 @@ bool syntax_is_opening_bracket(Language lang, char32_t c) { } // lookup the given string in the keywords table -static Keyword const *syntax_keyword_lookup(const KeywordList *all_keywords, char32_t const *str, size_t len) { +static Keyword const *syntax_keyword_lookup(const KeywordList *all_keywords, const char32_t *str, size_t len) { if (!len) return NULL; const KeywordList *list = &all_keywords[str[0] % 128]; @@ -165,7 +165,7 @@ static Keyword const *syntax_keyword_lookup(const KeywordList *all_keywords, cha } // does i continue the number literal from i-1 -static inline bool syntax_number_continues(Language lang, char32_t const *line, u32 line_len, u32 i) { +static bool syntax_number_continues(Language lang, const char32_t *line, u32 line_len, u32 i) { if (line[i] == '.') { if ((i && line[i-1] == '.') || (i < line_len-1 && line[i+1] == '.')) return false; // can't have two .s in a row @@ -200,7 +200,7 @@ static bool is_keyword(Language lang, char32_t c) { } // find how long this keyword would be (if this is a keyword) -static inline u32 syntax_keyword_len(Language lang, char32_t const *line, u32 i, u32 line_len) { +static u32 syntax_keyword_len(Language lang, const char32_t *line, u32 i, u32 line_len) { u32 keyword_end; for (keyword_end = i; keyword_end < line_len; ++keyword_end) { if (!is_keyword(lang, line[keyword_end])) @@ -210,7 +210,7 @@ static inline u32 syntax_keyword_len(Language lang, char32_t const *line, u32 i, } // highlighting for C, C++, and GLSL -static void syntax_highlight_c_cpp(SyntaxState *state_ptr, char32_t const *line, u32 line_len, SyntaxCharType *char_types, Language lang) { +static void syntax_highlight_c_cpp(SyntaxState *state_ptr, const char32_t *line, u32 line_len, SyntaxCharType *char_types, Language lang) { SyntaxState state = *state_ptr; bool in_preprocessor = (state & SYNTAX_STATE_CPP_PREPROCESSOR) != 0; bool in_string = (state & SYNTAX_STATE_CPP_STRING) != 0; @@ -375,7 +375,7 @@ static void syntax_highlight_c_cpp(SyntaxState *state_ptr, char32_t const *line, ); } -static void syntax_highlight_rust(SyntaxState *state, char32_t const *line, u32 line_len, SyntaxCharType *char_types) { +static void syntax_highlight_rust(SyntaxState *state, const char32_t *line, u32 line_len, SyntaxCharType *char_types) { u32 comment_depth = (((u32)*state & SYNTAX_STATE_RUST_COMMENT_DEPTH_MASK) / SYNTAX_STATE_RUST_COMMENT_DEPTH_MUL); bool in_string = (*state & SYNTAX_STATE_RUST_STRING) != 0; bool string_is_raw = (*state & SYNTAX_STATE_RUST_STRING_IS_RAW) != 0; @@ -601,7 +601,7 @@ static void syntax_highlight_rust(SyntaxState *state, char32_t const *line, u32 ); } -static void syntax_highlight_python(SyntaxState *state, char32_t const *line, u32 line_len, SyntaxCharType *char_types) { +static void syntax_highlight_python(SyntaxState *state, const char32_t *line, u32 line_len, SyntaxCharType *char_types) { (void)state; bool in_string = (*state & SYNTAX_STATE_PYTHON_STRING) != 0; bool string_is_dbl_quoted = (*state & SYNTAX_STATE_PYTHON_STRING_DBL_QUOTED) != 0; @@ -726,7 +726,7 @@ static bool is_tex_ident(char32_t c) { return is32_word(c) && !is32_digit(c) && c != '_'; } -static void syntax_highlight_tex(SyntaxState *state, char32_t const *line, u32 line_len, SyntaxCharType *char_types) { +static void syntax_highlight_tex(SyntaxState *state, const char32_t *line, u32 line_len, SyntaxCharType *char_types) { bool dollar = (*state & SYNTAX_STATE_TEX_DOLLAR) != 0; bool dollardollar = (*state & SYNTAX_STATE_TEX_DOLLARDOLLAR) != 0; bool verbatim = (*state & SYNTAX_STATE_TEX_VERBATIM) != 0; @@ -825,7 +825,7 @@ static void syntax_highlight_tex(SyntaxState *state, char32_t const *line, u32 l ); } -static void syntax_highlight_markdown(SyntaxState *state, char32_t const *line, u32 line_len, SyntaxCharType *char_types) { +static void syntax_highlight_markdown(SyntaxState *state, const char32_t *line, u32 line_len, SyntaxCharType *char_types) { bool multiline_code = (*state & SYNTAX_STATE_MARKDOWN_CODE) != 0; *state = (multiline_code * SYNTAX_STATE_MARKDOWN_CODE); @@ -1017,7 +1017,7 @@ static bool is_html_tag_char(char32_t c) { } // highlights XML and HTML -static void syntax_highlight_xml(SyntaxState *state, char32_t const *line, u32 line_len, SyntaxCharType *char_types, Language lang) { +static void syntax_highlight_xml(SyntaxState *state, const char32_t *line, u32 line_len, SyntaxCharType *char_types, Language lang) { bool comment = (*state & SYNTAX_STATE_HTML_COMMENT) != 0; bool in_sgl_string = false; // 'string' bool in_dbl_string = false; // "string" @@ -1131,7 +1131,7 @@ static void syntax_highlight_xml(SyntaxState *state, char32_t const *line, u32 l *state = (comment * SYNTAX_STATE_HTML_COMMENT); } -static void syntax_highlight_config(SyntaxState *state, char32_t const *line, u32 line_len, SyntaxCharType *char_types, bool is_ted_cfg) { +static void syntax_highlight_config(SyntaxState *state, const char32_t *line, u32 line_len, SyntaxCharType *char_types, bool is_ted_cfg) { bool string = (*state & SYNTAX_STATE_TED_CFG_STRING) != 0; if (line_len == 0) return; @@ -1212,7 +1212,7 @@ static void syntax_highlight_config(SyntaxState *state, char32_t const *line, u3 // highlighting for javascript, typescript, and JSON static void syntax_highlight_javascript_like( - SyntaxState *state, char32_t const *line, u32 line_len, SyntaxCharType *char_types, Language language) { + SyntaxState *state, const char32_t *line, u32 line_len, SyntaxCharType *char_types, Language language) { (void)state; bool string_is_template = (*state & SYNTAX_STATE_JAVASCRIPT_TEMPLATE_STRING) != 0; bool in_multiline_comment = (*state & SYNTAX_STATE_JAVASCRIPT_MULTILINE_COMMENT) != 0; @@ -1374,7 +1374,7 @@ static void syntax_highlight_javascript_like( *state |= SYNTAX_STATE_JAVASCRIPT_MULTILINE_COMMENT; } -static void syntax_highlight_java(SyntaxState *state_ptr, char32_t const *line, u32 line_len, SyntaxCharType *char_types) { +static void syntax_highlight_java(SyntaxState *state_ptr, const char32_t *line, u32 line_len, SyntaxCharType *char_types) { SyntaxState state = *state_ptr; bool in_string = false; bool in_multiline_comment = (state & SYNTAX_STATE_JAVA_MULTILINE_COMMENT) != 0; @@ -1499,7 +1499,7 @@ static void syntax_highlight_java(SyntaxState *state_ptr, char32_t const *line, } -static void syntax_highlight_go(SyntaxState *state_ptr, char32_t const *line, u32 line_len, SyntaxCharType *char_types) { +static void syntax_highlight_go(SyntaxState *state_ptr, const char32_t *line, u32 line_len, SyntaxCharType *char_types) { SyntaxState state = *state_ptr; bool string_is_raw = (state & SYNTAX_STATE_GO_RAW_STRING) != 0; bool in_string = string_is_raw; @@ -1643,7 +1643,7 @@ static void syntax_highlight_go(SyntaxState *state_ptr, char32_t const *line, u3 // Rather than returning colors, it returns a character type (e.g. comment) which can be converted to a color. // To highlight multiple lines, start out with a zeroed SyntaxState, and pass a pointer to it each time. // You can set char_types to NULL if you just want to advance the state, and don't care about the character types. -void syntax_highlight(SyntaxState *state, Language lang, char32_t const *line, u32 line_len, SyntaxCharType *char_types) { +void syntax_highlight(SyntaxState *state, Language lang, const char32_t *line, u32 line_len, SyntaxCharType *char_types) { switch (lang) { case LANG_NONE: if (char_types) diff --git a/ted.h b/ted.h index 448b0b1..9c115b4 100644 --- a/ted.h +++ b/ted.h @@ -616,6 +616,15 @@ void gl_geometry_rect(Rect r, u32 color_rgba); void gl_geometry_rect_border(Rect r, float border_thickness, u32 color); void gl_geometry_draw(void); GLuint gl_load_texture_from_image(const char *path); +// === syntax.c === +Language language_from_str(const char *str); +const char *language_to_str(Language language); +const char *language_comment_start(Language l); +const char *language_comment_end(Language l); +ColorSetting syntax_char_type_to_color_setting(SyntaxCharType t); +char32_t syntax_matching_bracket(Language lang, char32_t c); +bool syntax_is_opening_bracket(Language lang, char32_t c); +void syntax_highlight(SyntaxState *state, Language lang, const char32_t *line, u32 line_len, SyntaxCharType *char_types); char *buffer_contents_utf8_alloc(TextBuffer *buffer); diff --git a/text.c b/text.c index 4d04f56..291e047 100644 --- a/text.c +++ b/text.c @@ -439,7 +439,7 @@ v2 text_get_size_v2(Font *font, const char *text) { } -void text_get_size32(Font *font, char32_t const *text, u64 len, float *width, float *height) { +void text_get_size32(Font *font, const char32_t *text, u64 len, float *width, float *height) { TextRenderState render_state = text_render_state_default; render_state.render = false; for (u64 i = 0; i < len; ++i) { diff --git a/text.h b/text.h index 8f7db58..dbfb82c 100644 --- a/text.h +++ b/text.h @@ -58,7 +58,7 @@ void text_font_set_force_monospace(Font *font, bool force); // Get the dimensions of some text. void text_get_size(Font *font, const char *text, float *width, float *height); v2 text_get_size_v2(Font *font, const char *text); -void text_get_size32(Font *font, char32_t const *text, u64 len, float *width, float *height); +void text_get_size32(Font *font, const char32_t *text, u64 len, float *width, float *height); void text_utf8(Font *font, const char *text, double x, double y, u32 color); void text_utf8_anchored(Font *font, const char *text, double x, double y, u32 color, Anchor anchor); void text_char_with_state(Font *font, TextRenderState *state, char32_t c); -- cgit v1.2.3