init commit

main
Martin Fouilleul 3 years ago
commit 02fe867dfb
  1. 2933
      c_parser.cpp
  2. 230
      example/css/styles.css
  3. 319
      example/doc.groucho.txt
  4. 422
      example/doc.html
  5. 39
      example/fonts/CM-Unicode-Font-Licence
  6. 29
      example/fonts/GUST-FONT-LICENSE
  7. BIN
      example/fonts/Lato.woff
  8. 86
      example/fonts/OFL-LICENSE
  9. BIN
      example/fonts/cmunti.otf
  10. BIN
      example/fonts/cmunui.otf
  11. BIN
      example/fonts/latinmodern-math.otf
  12. 1698
      main.cpp
  13. 858
      scanner.cpp
  14. 190
      scanner.h

File diff suppressed because it is too large Load Diff

@ -0,0 +1,230 @@
@font-face
{
font-family: Lato;
font-style: normal;
font-weight: normal;
src: url(../fonts/Lato.woff);
}
@font-face
{
font-family: Maths;
font-style: italic;
font-weight: normal;
src: url(../fonts/cmunti.otf);
}
@font-face
{
font-family: Maths;
font-style: normal;
font-weight: normal;
src: url(../fonts/cmunui.otf);
}
@font-face
{
font-family: latin-modern-maths;
font-style: normal;
font-weight: normal;
src: url(../fonts/latinmodern-math.otf);
}
/*-------------------------------------------------
Body and misc styles
--------------------------------------------------*/
body
{
display:block;
background-size: 1800px 1197px;
margin-left:20px;
font-family: Lato, Helvetica, sans-serif;
font-size : 18px;
}
a
{
font-weight: bold;
text-decoration: none;
color: #555555;
}
a:hover
{
font-weight : bold;
color: #AAAAAA;
}
a img
{
border: none;
}
img
{
-ms-interpolation-mode: bicubic;
}
/*-------------------------------------------------
Maths styles
--------------------------------------------------*/
sup, sub {
vertical-align: baseline;
position: relative;
top: -0.4em;
}
sub {
top: 0.4em;
}
.regular
{
font-style: normal;
}
.maths
{
font-family: Maths;
font-style: italic;
font-size: 18px;
}
.maths .sqrt-symbol
{
font-family: Maths;
font-style: normal;
font-size:1.37em;
}
.maths .sqrt-arg
{
margin-left:-0.1em;
border-top: 0.1em solid black;
}
.maths .fraction
{
display:inline-block;
vertical-align:-0.8em;
text-align: center;
}
.maths .fraction .numerator
{
display:block;
}
.maths .fraction .denominator
{
display:block;
border-top: 1px solid;
}
.maths .n-sum-product
{
display:inline-block;
vertical-align:-1.2em;
text-align: center;
}
.maths .range-min, .maths .range-max
{
display:block;
font-size:0.8em;
}
.maths .range-max
{
line-height: 0.7em;
}
.maths .n-op-symbol
{
font-family: latin-modern-maths;
font-size:1.8em;
font-style:normal;
display:block;
}
.maths .op, .maths .rel, .maths .symbol
{
font-family: latin-modern-maths;
font-style: normal;
}
.maths .vector
{
font-family: latin-modern-maths;
font-style: italic;
}
.maths .digit, .maths .delim
{
font-style: normal;
}
/*-------------------------------------------------
Code styles
--------------------------------------------------*/
.code
{
font-family: Courier;
white-space: pre;
}
div.code, p.code
{
background-color: #CCCCCC;
margin-top: 20px;
margin-bottom: 20px;
padding-top: 10px;
padding-bottom: 10px;
padding-left: 10px;
padding-right: 10px;
overflow: scroll;
line-height: 125%;
}
.code .keyword
{
color:#AA00AA;
font-weight: bold;
}
.code .type
{
color:#CC5500;
/* font-weight: bold;*/
}
.code .preproc
{
color:#0055AA;
font-weight: bold;
}
.code .function-name
{
color:#2222FF;
}
.code .comment, .code .comment .keyword, .code .comment .type, .comment .code .preproc
{
color:#007700;
font-weight: normal;
}
.code .string
{
color: #AA0000;
};
.code .syntax-error
{
color:#FF0000;
font-style:italic;
}

@ -0,0 +1,319 @@
[html]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/styles.css"/>
</head>
<body>
[/html]
= Groucho =
(A clunky tool I made to mar[m]\r\Chi\r[/m] up blog posts)
------------------------------------------------\
== Intro ==
Groucho is a little tool to generate an HTML file from a text containing simplified and non-intrusive markup codes.
It was written in order to ease writing blog posts for my website. Its goal is to allow to write content along with formatting instructions, aiming at the least friction, thus avoiding the need to do a second pass manually adding html tags or using a more complex editing tool, a process that is repetitive and tedious, and makes the source text less readable.
As the content I'm writting on these posts is mainly technical, Groucho is built around three main goals\s:
- Easy presentation of normal content, including sections titles, paragraphs, lists, images, links.
- Formatting of basic maths or physics formulas, supporting maths symbols and notations.
- Syntax highlighting of C source code.
Some examples :
[c]*this is some text*[/c] \twill print in bold : *this is some text*.
[c]/this is some text/[/c] \twill print in italics : /this is some text/.
[c]_this is some text_[/c] \twill print underlined : _this is some text_.
[c][m]x_+ = \frac{{-b+\sqrt{b^2-4ac}}{2a}}[/m][/c] \twill print this well-known maths formula\s:
[m]x_+ = \frac{{-b+\sqrt{b^2-4ac}}{2a}}[/m]
=== Download and Usage ===
The source code of Groucho is available under a Public domain or MIT license (choose whichever you prefer). The fonts provided for the example document are covered by their own licenses (X11 license for CMU and the GUST font license for latin-modern-maths), which you can find in the font directory.
You can download the sources [url=https://www.forkingpaths.garden/download.php?id=15]HERE[/url]. Please keep in mind though that this was written as a quick side-project for my personal use, and hence is far for production quality. It is made available online only in the interest of the reader's curiosity. It may or may not be updated in the future.
After downloading and extracting the archive, you should be able to compile in the source directory with\s:
[code=none]cc -o groucho *.cpp[/code]
\
To use it you can run :
[code=none]./groucho in out[/code]
\
Where in and out are your input and output files. If no files are specified groucho will get its input from the standard input and output to the standard output. You can test groucho by running it on the sample [c]doc.groucho.txt[/c] file. It should output the present quick documentation.
------------------------------------------------\
== Basic principles ==
If not otherwise mentioned, a character in the input will produce the same character on the output. Some special characters and sequences will be interpreted as symbols and insert html entities in the html output. Some others will be interpreted as markups and insert html tags in the output stream. These tags contain [c]class[/c] attributes that are used by the browser, along with a css stylesheet, to style their content. A sample stylesheet is available with groucho, but it should be customizable at will (well, with css, you never know).
The symbols, the markups and their interpretations depends on the current mode of the interpreter which can be one of the four following modes\s:
- Text mode which is the default and doesn't require a markup.
- HTML mode, which is used to insert raw html.
- Maths mode, which is used to present simple mathematical formulas.
- Code mode, which is used to present syntax-highlighted C code.
By default, groucho will operate in text mode. You can switch to one of the other modes by using block tags, like [c][maths]...[/maths][/c], and all text inserted between these two tags will be interpreted in this mode.
------------------------------------------------\
== HTML mode ==
All text placed between the tags [c][html][/c] and [c][/html][/c] will be interpreted as HTML and will be outputed as-is.
To produce the basic tags needed for an HTML document, you should insert the following block at the beginning of your input file\s:
[c]
[html]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/styles.css"/>
</head>
<body>
[/html]
[/c]
Where you can also specify your CSS style sheet. To close those tags you should insert the following block at the end of your input file\s:
[c]
[html]
</body>
</html>
[/html]
[/c]
------------------------------------------------\
== Markups common to Text and Maths mode ==
Text is the default mode, which is used when the source content is not contained between a pair of matching block markups.
Maths mode can be used by enclosing text between the markup tags [c][maths][/c] and [c][/maths][/c], which will interpret its content as maths, and also place the output between [c]<div class="maths"></div>[/c] html tags. Alternatively, if one whishes to insert maths into the same line as normal text, the markup tags [c][m][/c] and [c][/m][/c] can be used, which will place the output between [c]<span class="maths"></span>[/c] html tags.
Text and maths mode share some of their markups, so we will present them together and then present their differences.
=== HTML entities ===
Some characters used by html tags will be replaced by html entities :
- [c]<[/c] will be replaced by [c]&lt;[/c] in maths mode, and in text mode if it is not followed by an alphabetic character, by a [c]/[/c] or by a [c]![/c]. Otherwise, it will be interpreted as the beginning of an html tag and the following text until the next matching [c]>[/c] will be output as-is.
- [c]>[/c] will be replaced by [c]&gt;[/c].
- [c]&[/c] will be replaced by [c]&amp;[/c].
=== Escape sequences ===
If a character is preceded by a [c]\[/c], it will be interpreted as follows :
- A [c]\n[/c] will be interpreted as an intentional line break and produce a [c]<br>[/c] tag.
- An end of line preceded by [c]\[/c] will be interpreted as a source-only line break and will be suppressed in the output.
- A [c]\t[/c] will output two em spaces [c]&emsp;&emsp[/c].
- A [c]\s[/c] will output a non breakable space [c]&nbsp;[/c].
In addition to that, there's a list of letters or words that will be interpreted as a special symbol or markup when preceded by [c]\[/c]\s:
- Symbols and special letters. These symbols will likely be more useful in maths sections and not all fonts support them, but they're available in text mode as well\s:
- [c]\star[/c] will output a [m]\star[/m]
- [c]\lowstar[/c] will output a [m]\lowstar[/m]
- [c]\div[/c] will output a [m]\div[/m]
- [c]\minus[/c] will output a [m]\minus[/m]
- [c]\cdot[/c] will output a [m]\cdot[/m]
- [c]\times[/c] will output a [m]\times[/m]
- [c]\otimes[/c] will output a [m]\otimes[/m]
- [c]\leq[/c] will output a [m]\leq[/m]
- [c]\geq[/c] will output a [m]\geq[/m]
- [c]\larr[/c] will output a [m]\larr[/m]
- [c]\rarr[/c] will output a [m]\rarr[/m]
- [c]\inf[/c] will output the symbol [m]\inf[/m]
- Greek letters:
- [c]\alpha[/c], [c]\beta[/c], [c]\gamma[/c], [c]\delta[/c], etc. : [m]\r\alpha \beta \gamma \delta\r[/m]
- [c]\Alpha[/c], [c]\Beta[/c], [c]\Gamma[/c], [c]\Delta[/c], etc. : [m]\r\Alpha \Beta \Gamma \Delta\r[/m]
- Font style markups, availables in both modes, change the style of text enclosed between a matching pair of them\s:
- [c]\b[/c] for \bbold\b.
- [c]\i[/c] for \iitalics\i.
- [c]\u[/c] for \uunderlined\u
- [c]\r[/c] is used to render text in roman in a section that would otherwise be in italics. It can be useful for function in maths blocks, as in [m]a = \rlog\r(b)[/m]
- A single letter not matched by one of the above escape sequences will be output as is and not be interpreted as part of a markup.
------------------------------------------------\
== Text mode ==
==== Additional markups ====
In text mode, a section title can be created by enclosing text in a matching pair of [c]=[/c] sequences. The section level corresponds to the number of [c]=[/c] signs\s:
[c]===== Exemple title =====[/c] in the input will produce the html [c]<h5>Exemple title</h5>[/c], resulting in\s:
===== Exemple title =====
A separating line can be created by a sequence of five or more hyphens : [c]-----[/c] will output the html tag [c]<hr/>[/c] resulting in\s:
-----
Paragraphs can be created by simply having two line breaks in a row.
The use of \\b, \\i and \\u markups can alternatively replaced by (respectively) \*, \/, and \_.
The following character sequences can be used to create symbols\s:
- [c]--[/c] : two hyphens in a row will create a dash\s: --
- [c]-->[/c] : one or more hyphens followed by a right angled bracket will produce a right arrow\s: -->
- [c]<--[/c] : a left angled bracket followed by one or more hyphens will produce a left arrow\s: <--
- [c]"[/c] : matching pairs of double quotes will be replaced by [c]&ldquo;[/c] and [c]&rdquo;[/c] as in "quoted".
==== Lists ====
A number of tabs followed by an hyphen, followed by a space, will be interpreted as a list item. The number of leading tabs determine the list depth when creating nested lists\s:
[c]- item 1
- item 2
- sub item 2.1
- sub item 2.2
- item 3
[/c]
will produce the following list :
- item 1
- item 2
- sub item 2.1
- sub item 2.2
- item 3
=== External content ===
==== Links ====
The markup tags [c][url=][/url][/c] can be used to create a link. You specifiy the URL of your link by adding it after the [c]=[/c] sign. The text of the link goes between the two markup tags.
For instance, [c][url=https://www.forkingpaths.garden]Home page[/url][/c] will create the following link : [url=https://www.forkingpaths.garden]Home page[/url] that will land you on Forking Paths's website.
==== Images ====
An image can be created with the tag [c][img=][/c]. Similarly to the URL tag, you specify the URL to you image after the [c]=[/c] sign. Hence, the following tag [c][img=https://www.forkingpaths.garden/img/logo_black.png][/c] will display Forking Paths' logo\s:
[img=https://www.forkingpaths.garden/img/logo_black.png]
------------------------------------------------\
== Maths mode ==
By default, operators and capital letters will be printed in roman with a math-specific font, while lower-case letters will be printed in italics. You can always reverse that by using \\r and \\i markups.
=== Default symbol interpretation ===
In maths mode, the following (non-escaped) sequences are recognized as symbol equivalents\s:
- [c]-[/c] is equivalent to [c]\minus[/c]
- [c]*[/c] is equivalent to [c]\times[/c]
- [c]/[/c] is equivalent to [c]\div[/c]
- [c]<=[/c] is equivalent to [c]\leq[/c]
- [c]>=[/c] is equivalent to [c]\geq[/c]
=== Exponents and indices ===
A character preceded by a [c]_[/c] will be printed as an index, as in [m]u_k[/m]. You can use a pair of curled brackets after the underscode to have a longer index, ie. this code : [c]C_{(i,j)}[/c] will produce this result : [m]C_{(i,j)}[/m].
Similarly, the character [c]^[/c] is used for exponent, as in [m]\re\r^{\thin\rj(2\pi\rf\thint+\phi)}[/m].
=== Vectors ===
[c]\vec[/c] : the character following this escape markup will be rendered with a vector arrow above it, as in [m]\vecv[/m].
=== Square roots ===
[c]\sqrt[/c] : a single character, or a sequence of characters contained in curly brackets, following this escape markup, will be printed as the arguments of a square root.
[m]\sqrt{a*(1-n)^2}[/m]
More precisely, this markup outputs the following HTML\s:
[c]<span class="sqrt-symbol">&radic;</span>
<span class="sqrt-arg">&thinsp;YourExpressionHere;&nbsp;</span>[/c]
The square root character is rendered as an HTML entity and the trailing top line of the square root is simulated by the top border of the [c]sqrt-arg[/c] span. Because HTML\/CSS layout is such a disaster zone, it is extremely likely that the square root and the top bar won't join and you will have to adjust the stylesheet to make it happen. There's not much we can do about it, until MathML is largely supported, except resorting to TeX generated images and the like...
=== Fraction ===
You can use the markup [c]\frac{{numerator}{denominator}}[/c] to generate a fraction. For instance,
[c]y = \frac{{x^2+1}{2}}[/c]
will generate the following equation :
[m]y = \frac{{x^2+1}{2}}[/m]
As stated above, depending on your font, you might want to adjust the [c]text-alignment[/c] property of the [c]fraction[/c] class to align the fraction line with the equal sign.
=== Sums and Products ===
\nsum and \nproduct can be used to create indexed sums or products. You need to specify the maximum index and the minimum index enclosed in curled brackets, like this\s:
[c]x = \nsum{{p}{k=1}}u_k[/c]
which produces this result :
[m]x = \nsum{{p}{k=1}}u_k[/m]
------------------------------------------------\
== Code mode ==
All text placed between the tags [c][html][/c] and [c][/html][/c] will be interpreted as C code, syntax highlighted and placed between the html tags [c]<div class="code"></div>[/c]. Alternatively, if one whishes to insert code into the same line as normal text, the markup tags [c][c][/c] and <span class="code">[\/c]</span> can be used, which will place the output between the html tags [c]<span class="code"></span>[/c]. Inline code won't be syntax highlighted though.
You can specify that you don't want the syntax highlighter to be run on a code block by using the opening [c][code=none][/c] mark instead of [c][code][/c]
The CSS styling of code section uses [c]white-space: pre;[/c] to preserve indentation and line breaks. It also uses a monospace font by default. The syntax highlighter performs a simplified syntactical analysis of the code and encloses elements in [c]<span></span>[/c] tags, with one of the following classes\s:
- [c]keyword[/c] is used for C keywords, except for built-in types.
- [c]type[/c] is used for built-in or user-defined types.
- [c]function-name[/c] is used to highlight functions in declarations and expressions.
- [c]comment[/c] is used to highlight comments.
- [c]label[/c] is used for labels and [c]goto[/c] instructions.
The syntax highlighter may fail on some cases, either because the syntax is in fact incorrect or because the code uses some unrecognized dialect, or because the parser stumbles upon a macro, or simply because it lacks context. As a matter of fact, C not being completely context free, and to avoid complicating the parser for such a simple task, the parser sometimes falls back to some (hopefully) reasonable assumption on what's going on. If it was a wrong guess, it will try to recover and continue to output non highlighted code until it can resynchronize, often after the next semicolon or at the begining of the next compound statement.
Here is an exemple of a code snippet :
[code]for(int i=0;i<count;i++)
{
// Get all my items by name in the hash table and print their value
int hash = HashFunction(names[i]);
my_struct* a = GetItem(hash);
printf("item %s = %i %s\n", names[i], a->value, a->unit);
}
[/code]
------------------------------------------------\
== Planned features ==
This is a list of features that will eventually be added along the way, as I need them and\/or find time to implement them\s:
- Output some useful info to stderr on errors.
- Clean-up the scanner code and reduce redundancies between modes.
- Add markups to control over font size, text justification, etc.
- Automatic non breakable space before colons.
- Tables
- Big parentheses for enclosing fractions
- Exponents that should work with those big parentheses
- More maths and logic symbols
- TeX mode for more involved equations. It would allow to insert TeX code that would generate an image and a corresponding [c]<img>[/c] tag.
- Add cast-expressions to the C parser, recognize /some/ C++ features, etc...
- Other langages for syntax highlighting : eg. bash scripts, Objective-C ?
[html]
</body>
</html>
[/html]

@ -0,0 +1,422 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/styles.css"/>
</head>
<body>
<h1> Groucho </h1>
<p>
(A clunky tool I made to mar<span class="maths"><span class="regular"><span class="greek-letter">&Chi;</span></span></span> up blog posts)
</p>
<p>
<hr/>
</p>
<h2> Intro </h2>
<p>
Groucho is a little tool to generate an HTML file from a text containing simplified and non-intrusive markup codes.
</p>
<p>
It was written in order to ease writing blog posts for my website. Its goal is to allow to write content along with formatting instructions, aiming at the least friction, thus avoiding the need to do a second pass manually adding html tags or using a more complex editing tool, a process that is repetitive and tedious, and makes the source text less readable.
</p>
<p>
As the content I'm writting on these posts is mainly technical, Groucho is built around three main goals&nbsp;:
</p>
<ul>
<li>Easy presentation of normal content, including sections titles, paragraphs, lists, images, links.</li>
<li>Formatting of basic maths or physics formulas, supporting maths symbols and notations.</li>
<li>Syntax highlighting of C source code.</li>
</ul>
<p>
Some examples :
</p>
<p>
<span class="code">*this is some text*</span> &emsp;&emsp;will print in bold : <b>this is some text</b>.<br>
<span class="code">/this is some text/</span> &emsp;&emsp;will print in italics : <i>this is some text</i>.<br>
<span class="code">_this is some text_</span> &emsp;&emsp;will print underlined : <u>this is some text</u>.
</p>
<p>
<span class="code">[m]x_+ = \frac{{-b+\sqrt{b^2-4ac}}{2a}}[/m]</span> &emsp;&emsp;will print this well-known maths formula&nbsp;:
</p>
<p>
<span class="maths">x<sub><span class="op">&thinsp;+&thinsp;</span></sub> <span class="op">&thinsp;=&thinsp;</span> <span class="fraction"><span class="numerator"><span class="op">&thinsp;&minus;&thinsp;</span>b<span class="op">&thinsp;+&thinsp;</span><span class="sqrt-symbol">&radic;</span><span class="sqrt-arg">&thinsp;b<sup>&thinsp;<span class="digit">2</span></sup><span class="op">&thinsp;&minus;&thinsp;</span><span class="digit">4</span>ac&nbsp;</span></span><span class="denominator"><span class="digit">2</span>a</span></span></span>
</p>
<br>
<h3> Download and Usage </h3>
<p>
The source code of Groucho is available under a Public domain or MIT license (choose whichever you prefer). The fonts provided for the example document are covered by their own licenses (X11 license for CMU and the GUST font license for latin-modern-maths), which you can find in the font directory.
</p>
<p>
You can download the sources <a href="https://www.forkingpaths.garden/download.php?id=15">HERE</a>
. Please keep in mind though that this was written as a quick side-project for my personal use, and hence is far for production quality. It is made available online only in the interest of the reader's curiosity. It may or may not be updated in the future.
</p>
<p>
After downloading and extracting the archive, you should be able to compile in the source directory with&nbsp;:
</p>
<div class="code">cc -o groucho *.cpp</div>
<p>
To use it you can run :
</p>
<div class="code">./groucho in out</div>
<p>
Where in and out are your input and output files. If no files are specified groucho will get its input from the standard input and output to the standard output. You can test groucho by running it on the sample <span class="code">doc.groucho.txt</span> file. It should output the present quick documentation.
</p>
<p>
<hr/>
</p>
<h2> Basic principles </h2>
<p>
If not otherwise mentioned, a character in the input will produce the same character on the output. Some special characters and sequences will be interpreted as symbols and insert html entities in the html output. Some others will be interpreted as markups and insert html tags in the output stream. These tags contain <span class="code">class</span> attributes that are used by the browser, along with a css stylesheet, to style their content. A sample stylesheet is available with groucho, but it should be customizable at will (well, with css, you never know).
</p>
<p>
The symbols, the markups and their interpretations depends on the current mode of the interpreter which can be one of the four following modes&nbsp;:
</p>
<ul>
<li>Text mode which is the default and doesn't require a markup.</li>
<li>HTML mode, which is used to insert raw html.</li>
<li>Maths mode, which is used to present simple mathematical formulas.</li>
<li>Code mode, which is used to present syntax-highlighted C code.</li>
</ul>
<p>
By default, groucho will operate in text mode. You can switch to one of the other modes by using block tags, like <span class="code">[maths]...[/maths]</span>, and all text inserted between these two tags will be interpreted in this mode.
</p>
<br>
<p>
<hr/>
</p>
<h2> HTML mode </h2>
<p>
All text placed between the tags <span class="code">[html]</span> and <span class="code">[/html]</span> will be interpreted as HTML and will be outputed as-is.<br>
To produce the basic tags needed for an HTML document, you should insert the following block at the beginning of your input file&nbsp;:
</p>
<p>
<span class="code">
[html]
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;link rel="stylesheet" href="css/styles.css"/&gt;
&lt;/head&gt;
&lt;body&gt;
[/html]
</span>
</p>
<p>
Where you can also specify your CSS style sheet. To close those tags you should insert the following block at the end of your input file&nbsp;:
</p>
<p>
<span class="code">
[html]
&lt;/body&gt;
&lt;/html&gt;
[/html]
</span>
</p>
<br>
<p>
<hr/>
</p>
<h2> Markups common to Text and Maths mode </h2>
<p>
Text is the default mode, which is used when the source content is not contained between a pair of matching block markups.
</p>
<p>
Maths mode can be used by enclosing text between the markup tags <span class="code">[maths]</span> and <span class="code">[/maths]</span>, which will interpret its content as maths, and also place the output between <span class="code">&lt;div class="maths"&gt;&lt;/div&gt;</span> html tags. Alternatively, if one whishes to insert maths into the same line as normal text, the markup tags <span class="code">[m]</span> and <span class="code">[/m]</span> can be used, which will place the output between <span class="code">&lt;span class="maths"&gt;&lt;/span&gt;</span> html tags.
</p>
<p>
Text and maths mode share some of their markups, so we will present them together and then present their differences.
</p>
<h3> HTML entities </h3>
<p>
Some characters used by html tags will be replaced by html entities :
</p>
<ul>
<li><span class="code">&lt;</span> will be replaced by <span class="code">&amp;lt;</span> in maths mode, and in text mode if it is not followed by an alphabetic character, by a <span class="code">/</span> or by a <span class="code">!</span>. Otherwise, it will be interpreted as the beginning of an html tag and the following text until the next matching <span class="code">&gt;</span> will be output as-is.</li>
<li><span class="code">&gt;</span> will be replaced by <span class="code">&amp;gt;</span>.</li>
<li><span class="code">&amp;</span> will be replaced by <span class="code">&amp;amp;</span>.</li>
</ul>
<h3> Escape sequences </h3>
<p>
If a character is preceded by a <span class="code">\</span>, it will be interpreted as follows :
</p>
<ul>
<li>A <span class="code">\n</span> will be interpreted as an intentional line break and produce a <span class="code">&lt;br&gt;</span> tag.</li>
<li>An end of line preceded by <span class="code">\</span> will be interpreted as a source-only line break and will be suppressed in the output.</li>
<li>A <span class="code">\t</span> will output two em spaces <span class="code">&amp;emsp;&amp;emsp</span>.</li>
<li>A <span class="code">\s</span> will output a non breakable space <span class="code">&amp;nbsp;</span>.</li>
</ul>
<p>
In addition to that, there's a list of letters or words that will be interpreted as a special symbol or markup when preceded by <span class="code">\</span>&nbsp;:
</p>
<ul>
<li>Symbols and special letters. These symbols will likely be more useful in maths sections and not all fonts support them, but they're available in text mode as well&nbsp;:
<ul>
<li><span class="code">\star</span> will output a <span class="maths"><span class="op">&thinsp;&ast;&thinsp;</span></span></li>
<li><span class="code">\lowstar</span> will output a <span class="maths"><span class="op">&thinsp;&lowast;&thinsp;</span></span></li>
<li><span class="code">\div</span> will output a <span class="maths"><span class="op">&thinsp;&div;&thinsp;</span></span></li>
<li><span class="code">\minus</span> will output a <span class="maths"><span class="op">&thinsp;&minus;&thinsp;</span></span></li>
<li><span class="code">\cdot</span> will output a <span class="maths"><span class="op">&thinsp;&middot;&thinsp;</span></span></li>
<li><span class="code">\times</span> will output a <span class="maths"><span class="op">&thinsp;&times;&thinsp;</span></span></li>
<li><span class="code">\otimes</span> will output a <span class="maths"><span class="op">&thinsp;&otimes;&thinsp;</span></span></li>
<li><span class="code">\leq</span> will output a <span class="maths"><span class="rel">&leq;</span></span></li>
<li><span class="code">\geq</span> will output a <span class="maths"><span class="rel">&geq;</span></span></li>
<li><span class="code">\larr</span> will output a <span class="maths"><span class="delim">&larr;</span></span></li>
<li><span class="code">\rarr</span> will output a <span class="maths"><span class="delim">&rarr;</span></span></li>
<li><span class="code">\inf</span> will output the symbol <span class="maths"><span class="symbol">&infin;</span></span></li>
</ul>
</li>
</ul>
<ul>
<li>Greek letters:
<ul>
<li><span class="code">\alpha</span>, <span class="code">\beta</span>, <span class="code">\gamma</span>, <span class="code">\delta</span>, etc. : <span class="maths"><span class="regular"><span class="greek-letter">&alpha;</span> <span class="greek-letter">&beta;</span> <span class="greek-letter">&gamma;</span> <span class="greek-letter">&delta;</span></span></span></li>
<li><span class="code">\Alpha</span>, <span class="code">\Beta</span>, <span class="code">\Gamma</span>, <span class="code">\Delta</span>, etc. : <span class="maths"><span class="regular"><span class="greek-letter">&Alpha;</span> <span class="greek-letter">&Beta;</span> <span class="greek-letter">&Gamma;</span> <span class="greek-letter">&Delta;</span></span></span></li>
</ul>
</li>
</ul>
<ul>
<li>Font style markups, availables in both modes, change the style of text enclosed between a matching pair of them&nbsp;:
<ul>
<li><span class="code">\b</span> for <b>bold</b>.</li>
<li><span class="code">\i</span> for <i>italics</i>.</li>
<li><span class="code">\u</span> for <u>underlined</u></li>
<li><span class="code">\r</span> is used to render text in roman in a section that would otherwise be in italics. It can be useful for function in maths blocks, as in <span class="maths">a <span class="op">&thinsp;=&thinsp;</span> <span class="regular">log</span><span class="delim">(</span>b<span class="delim">)</span></span></li>
</ul>
</li>
</ul>
<ul>
<li>A single letter not matched by one of the above escape sequences will be output as is and not be interpreted as part of a markup.</li>
</ul>
<br>
<br>
<p>
<hr/>
</p>
<h2> Text mode </h2>
<h4> Additional markups </h4>
<p>
In text mode, a section title can be created by enclosing text in a matching pair of <span class="code">=</span> sequences. The section level corresponds to the number of <span class="code">=</span> signs&nbsp;:
</p>
<p>
<span class="code">===== Exemple title =====</span> in the input will produce the html <span class="code">&lt;h5&gt;Exemple title&lt;/h5&gt;</span>, resulting in&nbsp;:<br>
</p>
<h5> Exemple title </h5>
<p>
A separating line can be created by a sequence of five or more hyphens : <span class="code">-----</span> will output the html tag <span class="code">&lt;hr/&gt;</span> resulting in&nbsp;:<br>
<hr/>
</p>
<p>
Paragraphs can be created by simply having two line breaks in a row.
</p>
<p>
The use of \b, \i and \u markups can alternatively replaced by (respectively) *, /, and _.
</p>
<p>
The following character sequences can be used to create symbols&nbsp;:
</p>
<ul>
<li><span class="code">--</span> : two hyphens in a row will create a dash&nbsp;: &mdash;</li>
<li><span class="code">--&gt;</span> : one or more hyphens followed by a right angled bracket will produce a right arrow&nbsp;: &rarr;</li>
<li><span class="code">&lt;--</span> : a left angled bracket followed by one or more hyphens will produce a left arrow&nbsp;: &larr;</li>
<li><span class="code">"</span> : matching pairs of double quotes will be replaced by <span class="code">&amp;ldquo;</span> and <span class="code">&amp;rdquo;</span> as in &ldquo;quoted&rdquo;.</li>
</ul>
<h4> Lists </h4>
<p>
A number of tabs followed by an hyphen, followed by a space, will be interpreted as a list item. The number of leading tabs determine the list depth when creating nested lists&nbsp;:
</p>
<p>
<span class="code">- item 1
- item 2
- sub item 2.1
- sub item 2.2
- item 3
</span>
</p>
<p>
will produce the following list :
</p>
<ul>
<li>item 1</li>
<li>item 2
<ul>
<li>sub item 2.1</li>
<li>sub item 2.2</li>
</ul>
</li>
<li>item 3</li>
</ul>
<h3> External content </h3>
<h4> Links </h4>
<p>
he markup tags <span class="code">[url=][/url]</span> can be used to create a link. You specifiy the URL of your link by adding it after the <span class="code">=</span> sign. The text of the link goes between the two markup tags.<br>
For instance, <span class="code">[url=https://www.forkingpaths.garden]Home page[/url]</span> will create the following link : <a href="https://www.forkingpaths.garden">Home page</a>
that will land you on Forking Paths's website.
</p>
<br>
<h4> Images </h4>
<p>
n image can be created with the tag <span class="code">[img=]</span>. Similarly to the URL tag, you specify the URL to you image after the <span class="code">=</span> sign. Hence, the following tag <span class="code">[img=https://www.forkingpaths.garden/img/logo_black.png]</span> will display Forking Paths' logo&nbsp;:
</p>
<p>
<img src="https://www.forkingpaths.garden/img/logo_black.png">
</p>
<br>
<p>
<hr/>
</p>
<h2> Maths mode </h2>
<p>
By default, operators and capital letters will be printed in roman with a math-specific font, while lower-case letters will be printed in italics. You can always reverse that by using \r and \i markups.
</p>
<h3> Default symbol interpretation </h3>
<p>
In maths mode, the following (non-escaped) sequences are recognized as symbol equivalents&nbsp;:
</p>
<ul>
<li><span class="code">-</span> is equivalent to <span class="code">\minus</span></li>
<li><span class="code">*</span> is equivalent to <span class="code">\times</span></li>
<li><span class="code">/</span> is equivalent to <span class="code">\div</span></li>
<li><span class="code">&lt;=</span> is equivalent to <span class="code">\leq</span></li>
<li><span class="code">&gt;=</span> is equivalent to <span class="code">\geq</span></li>
</ul>
<h3> Exponents and indices </h3>
<p>
A character preceded by a <span class="code">_</span> will be printed as an index, as in <span class="maths">u<sub>k</sub></span>. You can use a pair of curled brackets after the underscode to have a longer index, ie. this code : <span class="code">C_{(i,j)}</span> will produce this result : <span class="maths"><span class="regular">C</span><sub><span class="delim">(</span>i<span class="delim">,</span>j<span class="delim">)</span></sub></span>.
</p>
<p>
Similarly, the character <span class="code">^</span> is used for exponent, as in <span class="maths"><span class="regular">e</span><sup>&#8239;<span class="regular">j<span class="delim">(</span><span class="digit">2</span><span class="greek-letter">&pi;</span></span>f&#8239;t<span class="op">&thinsp;+&thinsp;</span><span class="greek-letter">&phi;</span><span class="delim">)</span></sup></span>.
</p>
<h3> Vectors </h3>
<p>
<span class="code">\vec</span> : the character following this escape markup will be rendered with a vector arrow above it, as in <span class="maths">v<span class="vector">&#8407;</span></span>.
</p>
<h3> Square roots </h3>
<p>
<span class="code">\sqrt</span> : a single character, or a sequence of characters contained in curly brackets, following this escape markup, will be printed as the arguments of a square root.
</p>
<p>
<span class="maths"><span class="sqrt-symbol">&radic;</span><span class="sqrt-arg">&thinsp;a<span class="op">&thinsp;&times;&thinsp;</span><span class="delim">(</span><span class="digit">1</span><span class="op">&thinsp;&minus;&thinsp;</span>n<span class="delim">)</span><sup>&thinsp;<span class="digit">2</span></sup>&nbsp;</span></span>
</p>
<p>
More precisely, this markup outputs the following HTML&nbsp;:
</p>
<p>
<span class="code">&lt;span class="sqrt-symbol"&gt;&amp;radic;&lt;/span&gt;
&lt;span class="sqrt-arg"&gt;&amp;thinsp;YourExpressionHere;&amp;nbsp;&lt;/span&gt;</span>
</p>
<p>
The square root character is rendered as an HTML entity and the trailing top line of the square root is simulated by the top border of the <span class="code">sqrt-arg</span> span. Because HTML/CSS layout is such a disaster zone, it is extremely likely that the square root and the top bar won't join and you will have to adjust the stylesheet to make it happen. There's not much we can do about it, until MathML is largely supported, except resorting to TeX generated images and the like...
</p>
<h3> Fraction </h3>
<p>
You can use the markup <span class="code">\frac{{numerator}{denominator}}</span> to generate a fraction. For instance,
</p>
<p>
<span class="code">y = \frac{{x^2+1}{2}}</span>
</p>
<p>
will generate the following equation :
</p>
<p>
<span class="maths">y <span class="op">&thinsp;=&thinsp;</span> <span class="fraction"><span class="numerator">x<sup>&thinsp;<span class="digit">2</span></sup><span class="op">&thinsp;+&thinsp;</span><span class="digit">1</span></span><span class="denominator"><span class="digit">2</span></span></span></span>
</p>
<p>
As stated above, depending on your font, you might want to adjust the <span class="code">text-alignment</span> property of the <span class="code">fraction</span> class to align the fraction line with the equal sign.
</p>
<h3> Sums and Products </h3>
<p>
<span class="n-sum-product"></span>&thinsp;and <span class="n-sum-product"></span>&thinsp;ct can be used to create indexed sums or products. You need to specify the maximum index and the minimum index enclosed in curled brackets, like this&nbsp;:
</p>
<p>
<span class="code">x = \nsum{{p}{k=1}}u_k</span>
</p>
<p>
which produces this result :
</p>
<p>
<span class="maths">x <span class="op">&thinsp;=&thinsp;</span> <span class="n-sum-product"><span class="range-max">p</span><span class="n-op-symbol">&sum;</span><span class="range-min">k<span class="op">&thinsp;=&thinsp;</span><span class="digit">1</span></span></span>&thinsp;u<sub>k</sub></span>
</p>
<p>
<hr/>
</p>
<h2> Code mode </h2>
<p>
All text placed between the tags <span class="code">[html]</span> and <span class="code">[/html]</span> will be interpreted as C code, syntax highlighted and placed between the html tags <span class="code">&lt;div class="code"&gt;&lt;/div&gt;</span>. Alternatively, if one whishes to insert code into the same line as normal text, the markup tags <span class="code">[c]</span> and <span class="code">[/c]</span> can be used, which will place the output between the html tags <span class="code">&lt;span class="code"&gt;&lt;/span&gt;</span>. Inline code won't be syntax highlighted though.
</p>
<p>
You can specify that you don't want the syntax highlighter to be run on a code block by using the opening <span class="code">[code=none]</span> mark instead of <span class="code">[code]</span>
</p>
<p>
The CSS styling of code section uses <span class="code">white-space: pre;</span> to preserve indentation and line breaks. It also uses a monospace font by default. The syntax highlighter performs a simplified syntactical analysis of the code and encloses elements in <span class="code">&lt;span&gt;&lt;/span&gt;</span> tags, with one of the following classes&nbsp;:
</p>
<ul>
<li><span class="code">keyword</span> is used for C keywords, except for built-in types.</li>
<li><span class="code">type</span> is used for built-in or user-defined types.</li>
<li><span class="code">function-name</span> is used to highlight functions in declarations and expressions.</li>
<li><span class="code">comment</span> is used to highlight comments.</li>
<li><span class="code">label</span> is used for labels and <span class="code">goto</span> instructions.</li>
</ul>
<p>
The syntax highlighter may fail on some cases, either because the syntax is in fact incorrect or because the code uses some unrecognized dialect, or because the parser stumbles upon a macro, or simply because it lacks context. As a matter of fact, C not being completely context free, and to avoid complicating the parser for such a simple task, the parser sometimes falls back to some (hopefully) reasonable assumption on what's going on. If it was a wrong guess, it will try to recover and continue to output non highlighted code until it can resynchronize, often after the next semicolon or at the begining of the next compound statement.
</p>
<p>
Here is an exemple of a code snippet :
</p>
<div class="code"><span class="keyword">for</span>(<span class="type">int </span>i=<span class="constant">0</span>;<span class="expression-statement">i&lt;count;</span>i++)
{
<span class="comment">// Get all my items by name in the hash table and print their value
</span>
<span class="type">int </span>hash = <span class="function-name">HashFunction</span>(<span class="arguments">names[i]</span>);
<span class="type">my_struct</span>* a = <span class="function-name">GetItem</span>(<span class="arguments">hash</span>);
<span class="expression-statement"><span class="function-name">printf</span>(<span class="arguments"><span class="string">"item %s = %i %s\n"</span>, <span class="arguments">names[i], <span class="arguments">a-&gt;value, <span class="arguments">a-&gt;unit</span></span></span></span>);
</span>}
</div>
<br>
<p>
<hr/>
</p>
<h2> Planned features </h2>
<p>
This is a list of features that will eventually be added along the way, as I need them and/or find time to implement them&nbsp;:
</p>
<ul>
<li>Output some useful info to stderr on errors.</li>
<li>Clean-up the scanner code and reduce redundancies between modes.</li>
<li>Add markups to control over font size, text justification, etc.</li>
<li>Automatic non breakable space before colons.</li>
<li>Tables</li>
<li>Big parentheses for enclosing fractions</li>
<li>Exponents that should work with those big parentheses</li>
<li>More maths and logic symbols</li>
<li>TeX mode for more involved equations. It would allow to insert TeX code that would generate an image and a corresponding <span class="code">&lt;img&gt;</span> tag.</li>
<li>Add cast-expressions to the C parser, recognize <i>some</i> C++ features, etc...</li>
<li>Other langages for syntax highlighting : eg. bash scripts, Objective-C ?</li>
</ul>
<br>
<br>
<br>
</body>
</html>

@ -0,0 +1,39 @@
X11 License
Andrey V. Panov (C) 2005
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, and/or sell copies of the Software, and to permit persons to
whom the Software is furnished to do so, provided that the above
copyright notice(s) and this permission notice appear in all copies of
the Software and that both the above copyright notice(s) and this
permission notice appear in supporting documentation.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
Except as contained in this notice, the name of a copyright holder shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization of
the copyright holder.
As a special exception, if you create a document which uses these fonts,
and embed these fonts or unaltered portions of these fonts into the
document, these fonts does not by itself cause the resulting document to
be covered by the X11 License. This exception does not however
invalidate any other reasons why the document might be covered by the
X11 License. If you modify these fonts, you may extend this exception to
your version of the fonts, but you are not obligated to do so. If you do
not wish to do so, delete this exception statement from your version.

@ -0,0 +1,29 @@
% This is version 1.0, dated 22 June 2009, of the GUST Font License.
% (GUST is the Polish TeX Users Group, http://www.gust.org.pl)
%
% For the most recent version of this license see
% http://www.gust.org.pl/fonts/licenses/GUST-FONT-LICENSE.txt
% or
% http://tug.org/fonts/licenses/GUST-FONT-LICENSE.txt
%
% This work may be distributed and/or modified under the conditions
% of the LaTeX Project Public License, either version 1.3c of this
% license or (at your option) any later version.
%
% Please also observe the following clause:
% 1) it is requested, but not legally required, that derived works be
% distributed only after changing the names of the fonts comprising this
% work and given in an accompanying "manifest", and that the
% files comprising the Work, as listed in the manifest, also be given
% new names. Any exceptions to this request are also given in the
% manifest.
%
% We recommend the manifest be given in a separate file named
% MANIFEST-<fontid>.txt, where <fontid> is some unique identification
% of the font family. If a separate "readme" file accompanies the Work,
% we recommend a name of the form README-<fontid>.txt.
%
% The latest version of the LaTeX Project Public License is in
% http://www.latex-project.org/lppl.txt and version 1.3c or later
% is part of all distributions of LaTeX version 2006/05/20 or later.

Binary file not shown.

@ -0,0 +1,86 @@
SIL OPEN FONT LICENSE
Version 1.1 - 26 February 2007
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting — in part or in whole — any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

Binary file not shown.

Binary file not shown.

1698
main.cpp

File diff suppressed because it is too large Load Diff

@ -0,0 +1,858 @@
//*****************************************************************
//
// $file: scanner.cpp $
// $author: Martin Fouilleul $
// $date: 24/12/2017 $
//
//*****************************************************************
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Martin Fouilleul
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/
#include<stdio.h>
#include<strings.h>
#include"scanner.h"
static int mode = MODE_TEXT;
const int MAX_LOOK_AHEAD = 15;
bool LookAhead(FILE* input, const char* string)
{
char buffer[MAX_LOOK_AHEAD+1];
int len = strlen(string);
int count = 0;
while(count<len && count<MAX_LOOK_AHEAD)
{
buffer[count] = getc(input);
if(buffer[count] != string[count])
{
break;
}
count++;
}
if(count != len)
{
//NOTE(martin): push back characters and return false
for(int i=count;i>=0;i--)
{
ungetc(buffer[i], input);
}
return(false);
}
else
{
return(true);
}
}
char Peek(FILE* input)
{
char c = getc(input);
ungetc(c, input);
return(c);
}
void CharacterToken(FILE* input, token* t)
{
t->type = CHAR;
t->c = getc(input);
}
void MarkupCharToken(markup_type type, token* t)
{
t->type = MARKUP;
t->markup.type = type;
t->markup.value = 0;
}
void SymbolToken(symbol_code code, token* t)
{
t->type = SYMBOL;
t->symbol.code = code;
}
void EndlToken(FILE* input, token* t)
{
getc(input);
t->type = ENDL;
}
void AttributedMarkupToken(FILE* input, markup_type type, token* t)
{
t->type = MARKUP;
t->markup.type = type;
int count = 0;
char c;
while(((c = getc(input)) != EOF) && count < MAX_MARKUP_TAG_LENGTH)
{
if(c==']')
{
t->markup.text[count] = '\0';
return;
}
t->markup.text[count] = c;
count++;
}
//error
return;
}
void BlockToken(FILE* input, token* t)
{
t->type = MARKUP;
if( LookAhead(input, "[c]"))
{
t->markup.type = CODE_INLINE_START;
mode = MODE_CODE_INLINE;
}
else if(LookAhead(input, "[m]"))
{
t->markup.type = MATHS_INLINE_START;
mode = MODE_MATHS_INLINE;
}
else if(LookAhead(input, "[/c]"))
{
t->markup.type = CODE_INLINE_STOP;
mode = MODE_TEXT;
}
else if(LookAhead(input, "[/m]"))
{
t->markup.type = MATHS_INLINE_STOP;
mode = MODE_TEXT;
}
else if(LookAhead(input, "[code="))
{
AttributedMarkupToken(input, CODE_BLOCK_START, t);
mode = MODE_CODE_BLOCK;
}
else if(LookAhead(input, "[code]"))
{
t->markup.type = CODE_BLOCK_START;
mode = MODE_CODE_BLOCK;
}
else if(LookAhead(input, "[html]"))
{
t->markup.type = HTML_BLOCK_START;
mode = MODE_HTML;
}
else if(LookAhead(input, "[/code]"))
{
t->markup.type = CODE_BLOCK_STOP;
mode = MODE_TEXT;
}
else if(LookAhead(input, "[/html]"))
{
t->markup.type = HTML_BLOCK_STOP;
mode = MODE_TEXT;
}
else if(LookAhead(input, "[maths]"))
{
t->markup.type = MATHS_BLOCK_START;
mode = MODE_MATHS_BLOCK;
}
else if(LookAhead(input, "[/maths]"))
{
t->markup.type = MATHS_BLOCK_STOP;
mode = MODE_TEXT;
}
else if(LookAhead(input, "[url="))
{
AttributedMarkupToken(input, URL_START, t);
}
else if(LookAhead(input, "[/url]"))
{
t->markup.type = URL_STOP;
}
else if(LookAhead(input, "[img="))
{
AttributedMarkupToken(input, IMG, t);
}
else
{
//TODO(martin): should issue a warning ?
CharacterToken(input, t);
}
}
void RepeatableMarkupToken(FILE* input, char m, markup_type type, token* t)
{
char c;
int count = 0;
while((c = getc(input)) == m)
{
count++;
}
ungetc(c, input);
t->type = MARKUP;
t->markup.type = type;
t->markup.value = count;
}
void LessToken(FILE* input, token* t)
{
t->html.text[0] = getc(input);
char c = Peek(input);
if( c == '!'
|| (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| c == '/')
{
bool quotes = false;
bool dquotes = false;
int count = 1;
while(((c = getc(input)) != EOF) && (count < MAX_HTML_TAG_LENGTH))
{
t->html.text[count] = c;
if(c == '>' && !quotes && !dquotes)
{
count++;
break;
}
else if(c == '\'' && !dquotes)
{
quotes = !quotes;
}
else if(c == '\"' && !quotes)
{
dquotes = !dquotes;
}
count++;
}
if(count >= MAX_HTML_TAG_LENGTH || c == EOF)
{
//issue an error
}
else
{
t->type = HTML_TAG;
t->html.text[count] = '\0';
}
}
else if(c == '-')
{
while(c == '-')
{
c = getc(input);
}
ungetc(c, input);
t->type = SYMBOL;
t->symbol.code = LARR;
}
else
{
ungetc(t->html.text[0], input);
CharacterToken(input, t);
}
}
void EscapeToken(FILE* input, token* t)
{
getc(input);
t->type = SYMBOL;
if(LookAhead(input, "star"))
{
t->symbol.code = STAR;
}
else if(LookAhead(input, "lowstar"))
{
t->symbol.code = LOSTAR;
}
else if(LookAhead(input, "minus"))
{
SymbolToken(SUBSTRACT, t);
}
else if(LookAhead(input, "div"))
{
t->symbol.code = DIVIDE;
}
else if(LookAhead(input, "cdot"))
{
t->symbol.code = CDOT;
}
else if(LookAhead(input, "times"))
{
t->symbol.code = MULTIPLY;
}
else if(LookAhead(input, "otimes"))
{
t->symbol.code = OTIMES;
}
else if(LookAhead(input, "leq"))
{
t->symbol.code = LEQ;
}
else if(LookAhead(input, "geq"))
{
t->symbol.code = GEQ;
}
else if(LookAhead(input, "inf"))
{
t->symbol.code = INF;
}
else if(LookAhead(input, "larr"))
{
t->symbol.code = LARR;
}
else if(LookAhead(input, "rarr"))
{
t->symbol.code = RARR;
}
else if(LookAhead(input, "vec"))
{
MarkupCharToken(VECTOR, t);
}
else if(LookAhead(input, "sqrt"))
{
MarkupCharToken(SQRT, t);
}
else if(LookAhead(input, "frac"))
{
MarkupCharToken(FRACTION, t);
}
else if(LookAhead(input, "nsum"))
{
MarkupCharToken(NSUM, t);
}
else if(LookAhead(input, "nprod"))
{
MarkupCharToken(NPRODUCT, t);
}
else if(LookAhead(input, "alpha"))
{
t->symbol.code = ALPHA;
}
else if(LookAhead(input, "beta"))
{
t->symbol.code = BETA;
}
else if(LookAhead(input, "gamma"))
{
t->symbol.code = GAMMA;
}
else if(LookAhead(input, "delta"))
{
t->symbol.code = DELTA;
}
else if(LookAhead(input, "epsilon"))
{
t->symbol.code = EPSILON;
}
else if(LookAhead(input, "zeta"))
{
t->symbol.code = ZETA;
}
else if(LookAhead(input, "eta"))
{
t->symbol.code = ETA;
}
else if(LookAhead(input, "theta"))
{
t->symbol.code = THETA;
}
else if(LookAhead(input, "iota"))
{
t->symbol.code = IOTA;
}
else if(LookAhead(input, "kappa"))
{
t->symbol.code = KAPPA;
}
else if(LookAhead(input, "lambda"))
{
t->symbol.code = LAMBDA;
}
else if(LookAhead(input, "mu"))
{
t->symbol.code = MU;
}
else if(LookAhead(input, "nu"))
{
t->symbol.code = NU;
}
else if(LookAhead(input, "xi"))
{
t->symbol.code = XI;
}
else if(LookAhead(input, "omicron"))
{
t->symbol.code = OMICRON;
}
else if(LookAhead(input, "pi"))
{
t->symbol.code = PI;
}
else if(LookAhead(input, "rho"))
{
t->symbol.code = RHO;
}
else if(LookAhead(input, "sigma"))
{
t->symbol.code = SIGMA;
}
else if(LookAhead(input, "tau"))
{
t->symbol.code = TAU;
}
else if(LookAhead(input, "upsilon"))
{
t->symbol.code = UPSILON;
}
else if(LookAhead(input, "phi"))
{
t->symbol.code = PHI;
}
else if(LookAhead(input, "chi"))
{
t->symbol.code = CHI;
}
else if(LookAhead(input, "psi"))
{
t->symbol.code = PSI;
}
else if(LookAhead(input, "omega"))
{
t->symbol.code = OMEGA;
}
else if(LookAhead(input, "Alpha"))
{
t->symbol.code = ALPHA_UPPER;
}
else if(LookAhead(input, "Beta"))
{
t->symbol.code = BETA_UPPER;
}
else if(LookAhead(input, "Gamma"))
{
t->symbol.code = GAMMA_UPPER;
}
else if(LookAhead(input, "Delta"))
{
t->symbol.code = DELTA_UPPER;
}
else if(LookAhead(input, "Epsilon"))
{
t->symbol.code = EPSILON_UPPER;
}
else if(LookAhead(input, "Zeta"))
{
t->symbol.code = ZETA_UPPER;
}
else if(LookAhead(input, "Eta"))
{
t->symbol.code = ETA_UPPER;
}
else if(LookAhead(input, "Theta"))
{
t->symbol.code = THETA_UPPER;
}
else if(LookAhead(input, "Iota"))
{
t->symbol.code = IOTA_UPPER;
}
else if(LookAhead(input, "Kappa"))
{
t->symbol.code = KAPPA_UPPER;
}
else if(LookAhead(input, "Lambda"))
{
t->symbol.code = LAMBDA_UPPER;
}
else if(LookAhead(input, "Mu"))
{
t->symbol.code = MU_UPPER;
}
else if(LookAhead(input, "Nu"))
{
t->symbol.code = NU_UPPER;
}
else if(LookAhead(input, "Xi"))
{
t->symbol.code = XI_UPPER;
}
else if(LookAhead(input, "Omicron"))
{
t->symbol.code = OMICRON_UPPER;
}
else if(LookAhead(input, "Pi"))
{
t->symbol.code = PI_UPPER;
}
else if(LookAhead(input, "Rho"))
{
t->symbol.code = RHO_UPPER;
}
else if(LookAhead(input, "Sigma"))
{
t->symbol.code = SIGMA_UPPER;
}
else if(LookAhead(input, "Tau"))
{
t->symbol.code = TAU_UPPER;
}
else if(LookAhead(input, "Upsilon"))
{
t->symbol.code = UPSILON_UPPER;
}
else if(LookAhead(input, "Phi"))
{
t->symbol.code = PHI_UPPER;
}
else if(LookAhead(input, "Chi"))
{
t->symbol.code = CHI_UPPER;
}
else if(LookAhead(input, "Psi"))
{
t->symbol.code = PSI_UPPER;
}
else if(LookAhead(input, "Omega"))
{
t->symbol.code = OMEGA_UPPER;
}
else if(LookAhead(input, "thin"))
{
t->symbol.code = THINSPACE;
}
else if(LookAhead(input, "b"))
{
MarkupCharToken(BOLD, t);
}
else if(LookAhead(input, "i"))
{
MarkupCharToken(ITALIC, t);
}
else if(LookAhead(input, "u"))
{
MarkupCharToken(UNDERLINE, t);
}
else if(LookAhead(input, "r"))
{
MarkupCharToken(REGULAR, t);
}
else
{
t->type = ESC;
t->c = getc(input);
}
}
void GetNextTokenInHTMLMode(FILE* input, token* t)
{
char c = Peek(input);
if(c==EOF)
{
t->type = ENDF;
return;
}
if(c == '[')
{
t->type = MARKUP;
if(LookAhead(input, "[/html]"))
{
t->markup.type = CODE_INLINE_STOP;
mode = MODE_TEXT;
}
else
{
CharacterToken(input, t);
}
}
else
{
CharacterToken(input, t);
}
}
void GetNextTokenInCodeMode(FILE* input, token* t)
{
char c = Peek(input);
if(c==EOF)
{
t->type = ENDF;
return;
}
if(c == '[')
{
t->type = MARKUP;
if(LookAhead(input, "[/c]"))
{
t->markup.type = CODE_INLINE_STOP;
mode = MODE_TEXT;
}
else if(LookAhead(input, "[/code]"))
{
t->markup.type = CODE_BLOCK_STOP;
mode = MODE_TEXT;
}
else
{
CharacterToken(input, t);
}
}
else
{
CharacterToken(input, t);
}
}
void GetNextTokenInMathsMode(FILE* input, token* t)
{
char c = Peek(input);
if(c==EOF)
{
t->type = ENDF;
return;
}
switch(c)
{
case ':':
getc(input);
MarkupCharToken(BOLD, t);
break;
case '*':
getc(input);
SymbolToken(MULTIPLY, t);
break;
case '-':
getc(input);
SymbolToken(SUBSTRACT, t);
break;
case '_':
getc(input);
MarkupCharToken(SUBSCRIPT, t);
break;
case '^':
getc(input);
MarkupCharToken(SUPERSCRIPT, t);
break;
case '\n':
EndlToken(input, t);
break;
case '[':
BlockToken(input, t);
break;
case '<':
getc(input);
c = Peek(input);
if(c == '=')
{
getc(input);
SymbolToken(LEQ, t);
}
else
{
ungetc('<', input);
LessToken(input, t);
}
break;
case '>':
getc(input);
c = Peek(input);
if(c == '=')
{
getc(input);
SymbolToken(GEQ, t);
}
else
{
ungetc('>', input);
CharacterToken(input, t);
}
break;
case '\\':
EscapeToken(input, t);
break;
default:
CharacterToken(input, t);
break;
}
}
void RepeatableHyphen(FILE* input, token* t)
{
char c;
int count = 1;
while((c = getc(input)) == '-')
{
count++;
}
t->type = SYMBOL;
if(c == '>')
{
t->symbol.code = RARR;
}
else
{
if(count <= 3)
{
t->symbol.code = EMDASH;
}
else
{
t->symbol.code = LINE;
}
ungetc(c, input);
}
}
void LookAheadForList(FILE* input, token* t)
{
char buffer[8];
bool plain = false;
int count = 0;
while(count < 8)
{
char c = getc(input);
buffer[count] = c;
count++;
if(c == '-')
{
if(LookAhead(input, " "))
{
t->type = MARKUP;
t->markup.type = LIST;
t->markup.value = count;
return;
}
else if(count==1 && LookAhead(input, "-"))
{
RepeatableHyphen(input, t);
return;
}
break;
}
else if(c != '\t')
{
break;
}
}
for(int i=count-1;i>0;i--)
{
ungetc(buffer[i], input);
}
t->type = CHAR;
t->c = buffer[0];
}
void GetNextTokenInTextMode(FILE* input, token* t)
{
char c = Peek(input);
if(c==EOF)
{
t->type = ENDF;
return;
}
switch(c)
{
case '=':
RepeatableMarkupToken(input, '=', SECTION, t);
break;
case '\t': case '-':
LookAheadForList(input, t);
break;
case '_':
getc(input);
MarkupCharToken(UNDERLINE, t);
break;
case '*':
getc(input);
MarkupCharToken(BOLD, t);
break;
case '/':
getc(input);
MarkupCharToken(ITALIC, t);
break;
case '\n':
EndlToken(input, t);
break;
case '[':
BlockToken(input, t);
break;
case '<':
LessToken(input, t);
break;
case '\\':
return EscapeToken(input, t);
default:
CharacterToken(input, t);
break;
}
}
void GetNextToken(FILE* input, token* t)
{
//NOTE(martin): finds the next token from input, effectively consumming characters
// if no token is found, those 'lookahead' characters are pushed back to the stream
switch(mode)
{
case MODE_HTML:
return(GetNextTokenInHTMLMode(input, t));
case MODE_CODE_INLINE: case MODE_CODE_BLOCK:
return(GetNextTokenInCodeMode(input, t));
case MODE_MATHS_INLINE: case MODE_MATHS_BLOCK:
return(GetNextTokenInMathsMode(input, t));
case MODE_TEXT: default:
GetNextTokenInTextMode(input, t);
}
}
void ResetScanMode()
{
mode = MODE_TEXT;
}

@ -0,0 +1,190 @@
//*****************************************************************
//
// $file: scanner.h $
// $author: Martin Fouilleul $
// $date: 24/12/2017 $
//
//*****************************************************************
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Martin Fouilleul
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/
#ifndef __SCANNER_H_
#define __SCANNER_H_
enum{MODE_TEXT, MODE_MATHS_INLINE, MODE_MATHS_BLOCK, MODE_CODE_INLINE, MODE_CODE_BLOCK, MODE_HTML};
const int MAX_HTML_TAG_LENGTH = 1023;
const int MAX_MARKUP_TAG_LENGTH = 1023;
struct html_tag
{
char text[MAX_HTML_TAG_LENGTH+1];
};
typedef enum {MATHS_INLINE_START,
MATHS_INLINE_STOP,
MATHS_BLOCK_START,
MATHS_BLOCK_STOP,
CODE_INLINE_START,
CODE_INLINE_STOP,
CODE_BLOCK_START,
CODE_BLOCK_STOP,
HTML_BLOCK_START,
HTML_BLOCK_STOP,
URL_START,
URL_STOP,
IMG,
SECTION,
UNDERLINE,
BOLD,
ITALIC,
REGULAR,
LIST,
SUBSCRIPT,
SUPERSCRIPT,
VECTOR,
SQRT,
FRACTION,
NSUM,
NPRODUCT } markup_type;
struct markup_tag
{
markup_type type;
union
{
int value;
char text[MAX_MARKUP_TAG_LENGTH+1];
};
};
typedef enum { MULTIPLY,
DIVIDE,
SUBSTRACT,
STAR,
LOSTAR,
CDOT,
OTIMES,
LEQ,
GEQ,
INF,
LARR,
RARR,
EMDASH,
LINE,
THINSPACE,
ALPHA,
BETA,
GAMMA,
DELTA,
EPSILON,
ZETA,
ETA,
THETA,
IOTA,
KAPPA,
LAMBDA,
MU,
NU,
XI,
OMICRON,
PI,
RHO,
SIGMA,
TAU,
UPSILON,
PHI,
CHI,
PSI,
OMEGA,
ALPHA_UPPER,
BETA_UPPER,
GAMMA_UPPER,
DELTA_UPPER,
EPSILON_UPPER,
ZETA_UPPER,
ETA_UPPER,
THETA_UPPER,
IOTA_UPPER,
KAPPA_UPPER,
LAMBDA_UPPER,
MU_UPPER,
NU_UPPER,
XI_UPPER,
OMICRON_UPPER,
PI_UPPER,
RHO_UPPER,
SIGMA_UPPER,
TAU_UPPER,
UPSILON_UPPER,
PHI_UPPER,
CHI_UPPER,
PSI_UPPER,
OMEGA_UPPER } symbol_code;
struct markup_symbol
{
symbol_code code;
};
typedef enum { CHAR, SYMBOL, ESC, ENDL, ENDF, MARKUP, HTML_TAG} token_type;
struct token
{
token_type type;
int line;
int col;
union
{
char c;
html_tag html;
markup_tag markup;
markup_symbol symbol;
};
};
void GetNextToken(FILE* input, token* t);
void ResetScanMode();
#endif //__SCANNER_H_
Loading…
Cancel
Save