Skip to content

Commit

Permalink
Improve xterm-256color support
Browse files Browse the repository at this point in the history
  • Loading branch information
bisqwit committed Jan 12, 2018
1 parent be9b7ba commit 05eaf3c
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 115 deletions.
63 changes: 57 additions & 6 deletions c.jsf
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
=Preproc_charContent fg_525 dim underline

# Comments within preprocessor statements
=PreprocComment fg_412 dim underline
=PreprocComment fg_412 dim underline italic
# Unknown preprocessor words
=PreprocUnknown underline fg_500 bg_543

Expand All @@ -110,7 +110,7 @@
=OpenACC_space underline fg_132

# Comments
=Comment fg_411
=Comment fg_411 italic

# Most operators () ? ~ . / : * / + - < >
=Special fg_033
Expand All @@ -121,6 +121,7 @@
=BisonDelim fg_430
=BisonKw fg_442 italic
=BisonComment fg_411
=BisonDollar fg_443 dim
=Re2cDelim fg_430
=Re2cEscape fg_202
=Re2cRangeDelim fg_024
Expand Down Expand Up @@ -153,8 +154,8 @@
=WRawString fg_242
=WRawStringContent fg_353
# Char constants
=Char fg_313
=CharContent fg_525
=Char fg_021
=CharContent fg_044
=WChar fg_331
=WCharContent fg_552

Expand All @@ -173,10 +174,13 @@
# All other identifiers
=Identifier fg_333

=BisonIdent fg_555 dim italic

=Hardcoded fg_000

# All unrecognized content
=Mystery fg_500 bg_542
#=Mystery bg_000 fg_033
=TrailingSpace bg_002

########################
Expand Down Expand Up @@ -677,7 +681,8 @@ done
"." period recolor=-1
"\"" stringbegin recolor=-1
"'" charbegin recolor=-1
"a-z$A-Z_" ident recolor=-1 buffer
"$" bison_ident recolor=-1
"a-zA-Z_" ident recolor=-1 buffer
"LU" maybe_wide recolor=-1 buffer
"u" maybe_unicode recolor=-1 buffer
"R" maybe_rawstring recolor=-1 buffer
Expand Down Expand Up @@ -781,7 +786,9 @@ done
" \n\t" bison_kw
"a-z" perc_ident recolor=-1 buffer
"{" bison_delim recolor=-1

:bison_ident BisonDollar
* idle noeat
"$0-9" bison_ident
:maybe_bison_end Special2
* idle noeat
"/" maybe_bison_comment markend
Expand Down Expand Up @@ -1063,6 +1070,45 @@ done
# Special identifiers
:ident Identifier
* idle noeat strings
"END" bisonident
"RETURN" bisonident
"WHILE" bisonident
"OR" bisonident
"AND" bisonident
"IF" bisonident
"VAR" bisonident
"EQ" bisonident
"NE" bisonident
"PP" bisonident
"MM" bisonident
"PL_EQ" bisonident
"MI_EQ" bisonident
"IDENTIFIER" bisonident
"NUMCONST" bisonident
"STRINGCONST" bisonident
"error" bisonident2
"program" bisonident
"functions" bisonident
"paramdecl" bisonident
"paramdecls" bisonident
"identifier1" bisonident
"colon1" bisonident
"semicolon1" bisonident
"cl_brace1" bisonident
"cl_bracket1" bisonident
"cl_parens1" bisonident
"statement1" bisonident
"statement" bisonident
"exprs1" bisonident
"expr1" bisonident
"p_expr1" bisonident
"com_stmt" bisonident
"var_defs" bisonident
"var_def1" bisonident
"exprs" bisonident
"c_expr1" bisonident
"expr" bisonident

"auto" type
"bool" type #c++
"char" type
Expand Down Expand Up @@ -1310,6 +1356,11 @@ done
")" idle


:bisonident BisonIdent
* idle noeat
:bisonident2 BisonKw
* idle noeat

:type Type
* idle noeat
:kw Keyword
Expand Down
93 changes: 64 additions & 29 deletions jsf.hh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Ad-hoc programming editor for DOSBox -- (C) 2011-03-08 Joel Yliluoma */
#include <string.h>
#include "vec_c.hh"

class JSF
{
Expand All @@ -17,7 +18,7 @@ public:
void Parse(FILE* fp)
{
char Buf[512]={0};
fprintf(stdout, "Parsing syntax file... "); fflush(stdout);
//fprintf(stdout, "Parsing syntax file... "); fflush(stdout);
TabType colortable;
char colors_sorted = 0;
states = 0; // NOTE: THIS LEAKS MEMORY
Expand All @@ -42,12 +43,12 @@ public:
else if(Buf[0] == ' ' || Buf[0] == '\t')
ParseStateLine(Buf, fp);
}
fprintf(stdout, "Binding... "); fflush(stdout);
//fprintf(stdout, "Binding... "); fflush(stdout);
BindStates();

for(unsigned n=0; n<colortable.size(); ++n) free(colortable[n].token);

fprintf(stdout, "Done\n"); fflush(stdout);
//fprintf(stdout, "Done\n"); fflush(stdout);
}
struct state;
struct ApplyState
Expand Down Expand Up @@ -98,7 +99,8 @@ public:
}
if(state.recolormark)
{
app.Recolor(state.markend, state.markbegin - state.markend, state.s->attr);
// markbegin & markend say how many characters AGO it was marked
app.Recolor(state.markend+1, state.markbegin - state.markend, state.s->attr);
}

option *o = state.s->options[state.c];
Expand Down Expand Up @@ -161,13 +163,17 @@ private:
}
};
/* std::vector<table_item> without STL, for Borland C++ */
#ifdef __GNUC__
using TabType = std::vector<table_item>;
#else
#define UsePlacementNew
#define T table_item
#define VecType TabType
#include "vecbase.hh"
#undef TabType
#undef VecType
#undef T
#undef UsePlacementNew
#endif

struct option
{
Expand All @@ -190,7 +196,6 @@ private:
char* namebegin = line;
while(*line && *line != ' ' && *line!='\t') ++line;
char* nameend = line;
char* line_end = NULL;
unsigned char fg256 = 0;
unsigned char bg256 = 0;
unsigned char flags = 0x00; // underline=1 dim=2 italic=4 bold=8 inverse=16 blink=32
Expand Down Expand Up @@ -228,7 +233,7 @@ private:
unsigned short c=0, i=0;
while(*line && *line != ' ' && *line != '\t') { c += 90u*(unsigned char)*line + i; i+=28; ++line; }
unsigned char code = ((c + 22u) / 26u) % 46u;
static const signed char actions[46] = { 11,30,3,1,31,18,29,23,13,36,15,25,7,2,28,-1,20,-1,24,9,16,26,-1,8,35,0,12,21,-1,5,10,17,22,33,32,34,4,14,-1,-1,6,27,-1,-1,19,37};
static const signed char actions[46] = { 10,30,2,1,31,19,29,23,13,36,15,25,7,3,28,-1,20,-1,24,9,16,27,-1,8,35,0,12,21,-1,5,11,17,22,33,32,34,4,14,-1,-1,6,26,-1,-1,18,37};
/*if(code >= 0 && code <= 45)*/ code = actions[code - 0];
switch(code >> 4) { case 0: fg256 = code&15; break;
case 1: bg256 = code&15; break;
Expand Down Expand Up @@ -414,10 +419,14 @@ private:
while(*line != '\0') ++line;
/*unsigned char* value_end = (unsigned char*) line;
*value_end++ = '\0';*/
table_item item;
item.token = key_begin;
item.state_name = value_begin;
o->stringtable.push_back(item);
if(*key_begin && *value_begin)
{
table_item item;
item.token = key_begin;
item.state_name = value_begin;
//fprintf(stdout, "String-table push '%s' '%s'\n", key_begin,value_begin);
o->stringtable.push_back(item);
}
}
sort(o->stringtable);
}
Expand Down Expand Up @@ -485,6 +494,33 @@ private:
}

// Converted state-names into pointers to state structures for fast access
void Remap(option*& o, TabType& state_cache, unsigned a, const char* statename)
{
if( ! o->name_mapped)
{
char* name = o->state_name;
o->state = findstate( state_cache, name );
if(!o->state)
{
fprintf(stdout, "Failed to find state called '%s' for index %u/256 in '%s'\n", name, a, statename);
}
o->name_mapped = 1;
for(TabType::iterator e = o->stringtable.end(),
t = o->stringtable.begin();
t != e;
++t)
{
char* name2 = t->state_name;
t->state = findstate( state_cache, name2 );
if(!t->state)
{
fprintf(stdout, "Failed to find state called '%s' for string table in target '%s' for '%s'\n", name2, name, statename);
}
// free(name2); - was not separately allocated
}
free(name);
}
}
void BindStates()
{
TabType state_cache;
Expand All @@ -502,35 +538,34 @@ private:
{
for(unsigned a=0; a<256; ++a)
{
option* o = states->options[a];
//tail:;
option*& o = states->options[a];
if(!o)
{
fprintf(stdout, "In state '%s', character state %u/256 not specified\n", states->name, a);
continue;
}
if( ! o->name_mapped)
Remap(o, state_cache, a, states->name);
while(o->noeat && o->recolor <= 1 && !o->buffer && !o->strings && !o->mark && !o->markend && !o->recolormark)
{
char* name = o->state_name;
o->state = findstate( state_cache, name );
if(!o->state)
unsigned long orig_attr = states->attr;
unsigned long new_attr = o->state->attr;
int had_recolor = o->recolor > 0;

o = o->state->options[a];
Remap(o, state_cache, a, o->state->name);

if(o->state->options[a]->recolor < 1 && (had_recolor || new_attr != orig_attr))
{
fprintf(stdout, "Failed to find state called '%s' for index %u/256 in '%s'\n", name, a, states->name);
o->state->options[a]->recolor = 1;
}
free(name);
o->name_mapped = 1;
for(TabType::iterator e = o->stringtable.end(),
t = o->stringtable.begin();
t != e;
++t)
{
name = t->state_name;
t->state = findstate( state_cache, name );
// free(name); - was not separately allocated
} } }
}
}
if(!states->next) break;
// Get the first-inserted state (last in chain) as starting-point.
states = states->next;
} }
}
}

static int TableItemCompareForSort(const void * a, const void * b)
{
Expand Down
Loading

0 comments on commit 05eaf3c

Please sign in to comment.