-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #764 from Golmote/prism-bison
Add support for Bison
- Loading branch information
Showing
10 changed files
with
315 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Prism.languages.bison = Prism.languages.extend('c', {}); | ||
|
||
Prism.languages.insertBefore('bison', 'comment', { | ||
'bison': { | ||
// This should match all the beginning of the file | ||
// including the prologue(s), the bison declarations and | ||
// the grammar rules. | ||
pattern: /^[\s\S]*?%%[\s\S]*?%%/, | ||
inside: { | ||
'c': { | ||
// Allow for one level of nested braces | ||
pattern: /%\{[\s\S]*?%\}|\{(?:\{[^}]*\}|[^{}])*\}/, | ||
inside: { | ||
'delimiter': { | ||
pattern: /^%?\{|%?\}$/, | ||
alias: 'punctuation' | ||
}, | ||
'bison-variable': { | ||
pattern: /[$@](?:<[^\s>]+>)?[\w$]+/, | ||
alias: 'variable', | ||
inside: { | ||
'punctuation': /<|>/ | ||
} | ||
}, | ||
rest: Prism.languages.c | ||
} | ||
}, | ||
'comment': Prism.languages.c.comment, | ||
'string': Prism.languages.c.string, | ||
'property': /\S+(?=:)/, | ||
'keyword': /%\w+/, | ||
'number': { | ||
pattern: /(^|[^@])\b(?:0x[\da-f]+|\d+)/i, | ||
lookbehind: true | ||
}, | ||
'punctuation': /%[%?]|[|:;\[\]<>]/ | ||
} | ||
} | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<h1>Bison</h1> | ||
<p>To use this language, use the class "language-bison".</p> | ||
|
||
<h2>Comments</h2> | ||
<pre><code>// Single-line comment | ||
/* Multi-line | ||
comment */</code></pre> | ||
|
||
<h2>C prologue and Bison declarations</h2> | ||
<pre><code>%{ | ||
#include <stdio.h> | ||
#include <math.h> | ||
int yylex (void); | ||
void yyerror (char const *); | ||
%} | ||
|
||
%define api.value.type {double} | ||
%token NUM | ||
%union { char *string; } | ||
%% | ||
%%</code></pre> | ||
|
||
<h2>Grammar rules</h2> | ||
<pre><code>%% | ||
exp: | ||
NUM { $$ = $1; } | ||
| exp exp '+' { $$ = $1 + $2; } | ||
| exp exp '-' { $$ = $1 - $2; } | ||
| exp exp '*' { $$ = $1 * $2; } | ||
| exp exp '/' { $$ = $1 / $2; } | ||
| exp exp '^' { $$ = pow($1, $2); } /* Exponentiation */ | ||
| exp 'n' { $$ = -$1; } /* Unary minus */ | ||
; | ||
|
||
$@1: %empty { a(); }; | ||
$@2: %empty { c(); }; | ||
$@3: %empty { d(); }; | ||
exp: $@1 "b" $@2 $@3 "e" { f(); }; | ||
%%</code></pre> | ||
|
||
<h2>Full example</h2> | ||
<pre><code>/* Mini Calculator */ | ||
/* calc.y */ | ||
|
||
%{ | ||
#include "heading.h" | ||
int yyerror(char *s); | ||
int yylex(void); | ||
%} | ||
|
||
%union{ | ||
int int_val; | ||
string* op_val; | ||
} | ||
|
||
%start input | ||
|
||
%token <int_val> INTEGER_LITERAL | ||
%type <int_val> exp | ||
%left PLUS | ||
%left MULT | ||
|
||
%% | ||
|
||
input: /* empty */ | ||
| exp { cout << "Result: " << $1 << endl; } | ||
; | ||
|
||
exp: INTEGER_LITERAL { $$ = $1; } | ||
| exp PLUS exp { $$ = $1 + $3; } | ||
| exp MULT exp { $$ = $1 * $3; } | ||
; | ||
|
||
%% | ||
|
||
int yyerror(string s) | ||
{ | ||
extern int yylineno; // defined and maintained in lex.c | ||
extern char *yytext; // defined and maintained in lex.c | ||
|
||
cerr << "ERROR: " << s << " at symbol \"" << yytext; | ||
cerr << "\" on line " << yylineno << endl; | ||
exit(1); | ||
} | ||
|
||
int yyerror(char *s) | ||
{ | ||
return yyerror(string(s)); | ||
}</code></pre> | ||
|
||
<h2>Known failures</h2> | ||
<p>There are certain edge cases where Prism will fail. | ||
There are always such cases in every regex-based syntax highlighter. | ||
However, Prism dares to be open and honest about them. | ||
If a failure is listed here, it doesn’t mean it will never be fixed. This is more of a “known bugs” list, just with a certain type of bug. | ||
</p> | ||
|
||
<h3>Comment-like substring</h3> | ||
<pre><code>"This string is // broken"</code></pre> | ||
|
||
<h3>Two levels of nesting inside C section</h3> | ||
<pre><code>{ | ||
if($1) { | ||
if($2) { | ||
|
||
} | ||
} | ||
} // <- Broken | ||
%% | ||
%%</code></pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
%{ | ||
#include <stdio.h> | ||
%} | ||
%code { | ||
if(foo) { | ||
|
||
} | ||
} | ||
%% | ||
exp: | ||
NUM { | ||
$$ = f($3, $4); | ||
@$.first_column = @1.first_column; | ||
$result = $left + $<itype>1; | ||
} | ||
%% | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["bison", [ | ||
["c", [ | ||
["delimiter", "%{"], | ||
["macro", ["#include ", ["string", "<stdio.h>"]]], | ||
["delimiter", "%}"] | ||
]], | ||
["keyword", "%code"], | ||
["c", [ | ||
["delimiter", "{"], | ||
["keyword", "if"], ["punctuation", "("], "foo", ["punctuation", ")"], | ||
["punctuation", "{"], ["punctuation", "}"], | ||
["delimiter", "}"] | ||
]], | ||
["punctuation", "%%"], | ||
["property", "exp"], ["punctuation", ":"], | ||
"\r\n\tNUM ", | ||
["c", [ | ||
["delimiter", "{"], | ||
["bison-variable", ["$$"]], ["operator", "="], | ||
["function", "f"], ["punctuation", "("], | ||
["bison-variable", ["$3"]], ["punctuation", ","], | ||
["bison-variable", ["$4"]], ["punctuation", ")"], ["punctuation", ";"], | ||
["bison-variable", ["@$"]], ["punctuation", "."], "first_column ", ["operator", "="], | ||
["bison-variable", ["@1"]], ["punctuation", "."], "first_column", ["punctuation", ";"], | ||
["bison-variable", ["$result"]], ["operator", "="], | ||
["bison-variable", ["$left"]], ["operator", "+"], | ||
["bison-variable", ["$", ["punctuation", "<"], "itype", ["punctuation", ">"], "1"]], ["punctuation", ";"], | ||
["delimiter", "}"] | ||
]], | ||
["punctuation", "%%"] | ||
]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for C inside Bison, along with special Bison variables. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Foobar | ||
/* Foo | ||
bar */ | ||
%% | ||
// Foobar | ||
/* Foo | ||
bar */ | ||
%% | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["bison", [ | ||
["comment", "// Foobar"], | ||
["comment", "/* Foo\r\nbar */"], | ||
["punctuation", "%%"], | ||
["comment", "// Foobar"], | ||
["comment", "/* Foo\r\nbar */"], | ||
["punctuation", "%%"] | ||
]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for comments. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
%union | ||
%token | ||
%% | ||
exp: %empty | ||
%% | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["bison", [ | ||
["keyword", "%union"], | ||
["keyword", "%token"], | ||
["punctuation", "%%"], | ||
["property", "exp"], ["punctuation", ":"], | ||
["keyword", "%empty"], | ||
["punctuation", "%%"] | ||
]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for keywords. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
42 | ||
0 | ||
0xBadFace | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["number", "42"], | ||
["number", "0"], | ||
["number", "0xBadFace"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for numbers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
%% | ||
foo: | ||
bar_42: | ||
$@1: | ||
%% | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["bison", [ | ||
["punctuation", "%%"], | ||
["property", "foo"], ["punctuation", ":"], | ||
["property", "bar_42"], ["punctuation", ":"], | ||
["property", "$@1"], ["punctuation", ":"], | ||
["punctuation", "%%"] | ||
]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for properties. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
%% | ||
foo: 'foo' "foo"; | ||
bar: '\'' "\""; | ||
%% | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["bison", [ | ||
["punctuation", "%%"], | ||
["property", "foo"], ["punctuation", ":"], | ||
["string", "'foo'"], ["string", "\"foo\""], ["punctuation", ";"], | ||
["property", "bar"], ["punctuation", ":"], | ||
["string", "'\\''"], ["string", "\"\\\"\""], ["punctuation", ";"], | ||
["punctuation", "%%"] | ||
]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for strings. |