Skip to content

Simple examples

Vivek edited this page Feb 7, 2018 · 4 revisions
#include <iostream>
#include "seephit.h"
using std::cerr;

int main()
{
  constexpr auto parser =
  R"*(
    <span >
      <p  color="red" height='10' >{{name}} is a {{profession}} in {{city}}</p  >
    </span>
    )*"_html;
    
  REPORT_ERRORS(parser);
  
  spt::tree spt_tree(parser);
  spt::template_vals dct;
  spt::template_funs dctFuns;
    
  dct["name"] = "Mary";
  dct["profession"] = "doctor";
  dct["city"] = "London";
  
  spt_tree.root().render(cerr, dct, dctFuns);   
  cerr << endl;
  
  dct["city"] = "New York";
  dct["name"] = "John";
  dct["profession"] = "janitor";
    
  spt_tree.root().render(cerr, dct, dctFuns);   
  cerr << endl;    
}

produces the following output

<span>
  <p height='10' color='red'>
    Mary is a doctor in London
  </p>
</span>

<span>
  <p height='10' color='red'>
    John is a janitor in New York
  </p>
</span>
The program will fail to compile if the HTML is malformed - We attempt to make the compiler generate the most sensible error message:

For example, the following fragment:

<DIV>
This is a bad closing tag
</DIVV>

Generates the following compile errors in gcc:

$ g++  --std=c++17  -Wall main.cpp
In file included from seephit.h:6:0,
                 from main.cpp:2:
parse_error.h: In instantiation of 'constexpr spt::Error<ROW, COL, WHAT>::Error() 
[with int ROW = 4; int COL = 7; WHAT = spt::Mismatched_Close_Tag]':
main.cpp:14:3:   required from here
parse_error.h:43:15: error: incompatible types in assignment of 
'const int' to 'char [0]'
     SPTParser = fatal;
     ~~~~~~~~~~^~~~~~~

Some complicated template magic has been implemented to show the row and column in the text where the error occured.

gcc actually prints ROW = xxx and COL = xxx, which is great!

If your IDE does background parsing, it will indicate that your HTML template is malformed as you type it.

Clone this wiki locally