Leo<HTML>

Lexer Class

JS - Reference
JS Syntax
Output
Leohtml Class
File Class
Get_tmp_file Function
Line Class
Tag Class
Lexer Class
    Concept
    Interface
    Example
Rebuilt_file Function

Sources of lexer.html:
  
     
 
<include file="top.lhhtml"> 
 
<lconf pos="/lexer" pagename="lexer"> 
 
<section link="/lexer/concept" local="concept"> 
 
<locale> 
  <locale language="en"> 
 
The first step of parsing a HTML file is to lex it. The lexer splitts up the file in tokens. 
In HTML it is simple, there are only 2 tokens: a Tag, or not a Tag.<br> 
But the <leohtml> lexer is a little bit more complex. The compiler 
tries prevent the code for reformating. <br> 
That means that your coding style won't be touched if it is possible. 
Have a look at some of the generated output files, and you will see it.<br> 
Therefor the lexer stores additional informations in it's output file. 
 
  </locale> 
  <locale language="de"> 
 
Der erste Schritt beim Parsen einer HTML Datei ist es diese zu lexen. Das Lexer teilt 
die Datei in Tokens auf. Bei HTML ist es einfach, es gibt nur 2 Tokens: ein Tag und kein Tag.<br> 
Aber der Lexer von <leohtml> is ein wenig komplexer. Der Kompiler versucht die 
ursprüngliche Form der Datei beizubehalten. <br> 
Das bedeuted, dass dein Code seine Formatierung beibehält, sofern dies möglich ist. 
Sieh dir einige vom Kompiler generierte Dateien an und du wirst sehen, was ich meine.<br> 
Um dies zu ermöglichen speichert der Lexer zusätzliche Informationen in seiner Ausgabedatei. 
 
  </locale> 
</locale> 
 
<br> 
<br> 
 
<example mode="fixed" title-de="Originaler Code:" title-en="Original Code:"> 
<char latin1> 
 
<p> 
 <leohtml> is a HTML precompiler for <b>static</b> home page generation. 
 It detects special HTML tags in your page and generates HTML code out of it. 
</p> 
 
</char> 
</example> 
 
<br> 
 
<example mode="fixed" title-de="Gelexter Code:" title-de="Lexed Code:"> 
<char latin1> 
 
dev2:index.lhtml:9:0:: 
dev2:index.lhtml:10:0:p:<p> 
dev2:index.lhtml:11:0:: 
dev2:index.lhtml:11:1:leohtml:<leohtml> 
dev2:index.lhtml:11:10:: is a HTML precompiler for 
dev2:index.lhtml:11:37:b:<b> 
dev2:index.lhtml:11:40::static 
dev2:index.lhtml:11:46:/b:</b> 
dev2:index.lhtml:11:50:: home page generation. 
dev2:index.lhtml:12:0:: It detects special HTML tags in your page and generates HTML code out of it. 
dev2:index.lhtml:13:0:/p:</p> 
dev2:index.lhtml:14:0:: 
 
</char> 
</example> 
 
<br> 
<br> 
 
<locale> 
  <locale language="en"> 
 
<p> 
The first field is the directory where the file was found. This field can be empty. <br> 
The second field is the name of the file, that was original included. <br> 
The third field is the line number where this tag was found. <br> 
The fourth field is the column number where the tag was locacated within this line. <br> 
The fifth  field is the name of the tag that was found. If there was no tag, this file is empty. <br> 
And the last file is the string that was found. This can be a tag too. If this field 
 is empty it can be a line break.<br> 
</p> 
 
<p> 
Now you can write an extra parser to parse each of these lines, or you 
use the <link path="/line">, that does this stuff for you. And a <code>Line</code> 
will be accepted by the <link path="/tag">. All these usefule classes are designed to work together. 
Your life will be easier if you use them. 
</p> 
 
  </locale> 
  <locale language="de"> 
 
<p> 
Das erste Feld ist das Verzeichnis in dem die Datei gefunden wurde. Dieses Feld kann leer sein. <br> 
Das zweite Feld ist der Name der Datei die ursprünglich eingebunden wurde. <br> 
Das dritte Feld ist die Nummer der Zeile in der das Tag gefunden wurde. <br> 
Das vierte Feld ist die Nummer der Spalte in der das Tag gefunden wurde. <br> 
Das fünfte Feld ist der Name des Tags, wenn die aktuelle gelexte Zeile ein Tag beinhaltet. 
 ansonsten ist dieses Feld leer.<br> 
Und das letzte Feld ist das Tag, oder ein andere Zeichenkette, die gefunden wurde. Ist dieses 
 Feld leer, dann handelt es sich meißt um einen Zeilenumbruch.<br> 
</p> 
 
<p> 
Nun kannst du wiederum einen extra Parser schreiben, der jede dieser Zeilen parst, 
oder du verwendest die <link path="/line">, die dies für dich erledigt. Und eine 
<code>Line</code> wird von der <link path="/tag"> akzeptiert. All diese nützlichen 
Klassen wurde entworfen um zusammen zu arbeiten. Dein Leben wird leichter sein, 
wenn die sie verwendest. 
</p> 
 
  </locale> 
</locale> 
 
</section> 
 
 
<!-- **************************** --> 
 
<section link="/lexer/interface" local="interface"> 
 
<definition title-de="Lexer Klasse Definition" title-en="Lexer Class Definition"> 
<include file="lexer.def"> 
</definition> 
 
<br> 
<br> 
 
<locale> 
  <locale language="en"> 
 
<p> 
The lexer get's an input file and produeces an output file. 
If you now the current directory you can set it optional. 
</p> 
 
<p> 
After constructing the <code>Lexer</code> class check if it valid. 
If it is not valid you can get the error message via the <b>get_error()</b> function. 
</p> 
 
<p> 
The third step is to lex the file. This will be done by the <b>lex()</b> class. 
After lexing the file check again, if the lexer is valid. It is possible that 
there occoured an error while lexing. 
</p> 
 
  </locale> 
  <locale language="de"> 
 
<p> 
Der Lexer erhält eine Eingabe-Datei und schreibt den Code in die Ausgabe-Datei. 
Wenn du das momentane Verzeichnis weißt, dann kannst du dieses optional auch angeben. 
</p> 
 
<p> 
Nach dem du die <code>Lexer</code> Klasse konstruiert hast überprüfe 
mit der <b>is_valid()</b> Funktion, ob sie gültig ist. Ist sie nicht gültig, 
dann erhälst du eine Fehlernachricht über die <b>get_error()</b> Funktion. 
</p> 
 
<p> 
Der dritte Schritt ist es den Lexer zu starten. Dies geschieht mit der <b>lex()</b> 
Funktion. Überprüfe auch dannach mit der <b>is_valid()</b> Funktion, ob kein Fehler 
aufgetreten ist. 
</p> 
 
  </locale> 
</locale> 
 
 
</section> 
 
 
<!-- ******************************* --> 
 
 
<section link="/lexer/example" local="example"> 
 
<locale> 
  <locale language="en"> 
 
Even if this is an reference, and I didn't wanted to add bigger 
examples here, I think it is better to add one here. This example 
shows how the various classes are working together. 
 
<p> 
This example is a simple filter. It searches for all tags within a 
HTML file and erases them.  
</p> 
 
  </locale> 
  <locale language="de"> 
 
Selbst wenn dies eine Referenz ist und ich ursprünglich keine 
großen Beispiele hier hinzufügen wollte, denke ich doch das dies 
hier an dieser Stelle besser ist wenn hier gezeigt wird wie die 
verscheidenen Klassen zusammenarbeiten. 
 
<p> 
Diese Beispiel ist ein Tag Filter. Es löscht alle Tags aus einer 
HTML Datei und gibt nur den Text aus. 
</p> 
 
  </locale> 
</locale> 
 
<example mode="fixed"> 
<char latin1> 
 
 
function html2text( infile_name ) { 
   
  // try open file 
  var infile = new File( infile_name, "r", true, true ); 
 
  if( !infile.is_valid() ) { 
    message( "ERROR: cannot open file", infile.get_name() ); 
    return; 
  } 
 
  // lex the file 
 
  // create a tmp file 
  var tmp = new File( get_tmp_file(), "rwt", false, false ); 
 
  var lexer = new Lexer( infile, tmp ); 
 
  if( ! lexer.is_valid() ) { 
    message( "ERROR: cannot lex file:", lexer.get_error() ); 
    return; 
  } 
 
  lexer.lex();  
 
  if( ! lexer.is_valid() ) { 
    message( "ERROR: cannot lex file:", lexer.get_error() ); 
    return; 
  } 
 
  tmp.clear(); 
  tmp.seekg(0);  // spool back to begin 
 
  while( ! tmp.eof() ) { 
 
    var line = new Line( tmp.getline() ); 
 
    // ignore invalid lines 
    if( ! line.is_valid() ) 
      continue; 
 
    if( line.tag_type != "" ) { 
 
      // here you can now inspekt the tag 
      // it is not necessary but a good  
      // example 
        
      var tag = new Tag( line ); 
 
      if( tag.is_valid() ) { 
 
        var m = "Tag: " + tag.get_tag_type();  // the message string 
 
        for( var i = 0 ; i < tag.get_number_of_options() ; i++ ) { 
          m = m + " | Option: " + tag.get_option_name( i )  
	    + " Value: "  + tag.get_option_value( i ); 
        } 
 
        message( m ); 
 
      } 
 
 
    } else { 
 
      // Line is not a tag, print the stuff out 
      print( line.tag ); 
    } 
 
  } 
 
} 
 
</char> 
</example> 
 
</section> 
 
 
                  
    



This page was created by King Leo . Page generator was Leo<HTML> version 0.99.0 .