Skip to content

Commit

Permalink
Make the OMCC Modelica lexer a single package
Browse files Browse the repository at this point in the history
The lexer includes almost all dependencies, requiring no
OMCC files except this generated lexer.
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Jun 29, 2015
1 parent 4148ee7 commit 2e69619
Show file tree
Hide file tree
Showing 10 changed files with 634 additions and 228 deletions.
401 changes: 384 additions & 17 deletions openmodelica/omcc/LexerCode.tmo

Large diffs are not rendered by default.

29 changes: 11 additions & 18 deletions openmodelica/omcc/LexerGenerator.mo
Expand Up @@ -153,7 +153,7 @@ print(intString(i) + " is token " + tokenName + "\n");
if (debug==true) then
print("\n BEGIN at" + intString(valBegin));
end if;
cp := " mm_startSt = " + intString(valBegin) +";";
cp := "\n mm_startSt = " + intString(valBegin) +";";
resTable := cp::resTable;
end if;

Expand All @@ -167,15 +167,13 @@ print(intString(i) + " is token " + tokenName + "\n");
end if;

if tokenName <> "" then
cp := "\n act2 = Token.";
cp := "\n tok = TOKEN(\"";
resTable := tokenName::cp::resTable;
cp := ";\n tok = OMCCTypes.TOKEN(\"";
resTable := tokenName::cp::resTable;
cp := "\",act2,fileContents,mm_pos-buffer,buffer,lineNrStart,mm_ePos+1,mm_linenr,mm_sPos+1);\n then tok;\n";
resTable := cp::resTable;
cp := ",fileContents,mm_pos-buffer,buffer,lineNrStart,mm_ePos+1,mm_linenr,mm_sPos+1);\n then tok;\n";
resTable := cp::tokenName::"\",TokenId."::resTable;
else
//print("NONE");
cp := "\n then OMCCTypes.noToken;\n";
cp := "\n then noToken;\n";
resTable := cp::resTable;
end if;

Expand Down Expand Up @@ -423,10 +421,10 @@ function buildTokens
output String result;
protected
String cp,re,flexCode,s;
Integer numMatches=0;
// Integer numMatches=0;
list<String> resTable,tokens,tokens2,tmp;
algorithm
cp := "encapsulated package Token";
cp := "type TokenId = enumeration(\n ";
resTable := cp::{};
tokens := {};

Expand All @@ -438,16 +436,11 @@ algorithm
tokens := s :: tokens;
end if;
end for;
tokens2 := List.sortedUnique(List.sort(tokens, Util.strcmpBool), stringEqual);
numMatches := listLength(tokens2);
tokens := {};
for str in tokens2 loop
tokens := ("\n constant Integer " + str + "=" + String(numMatches) + ";") :: tokens;
numMatches := numMatches - 1;
end for;
cp := stringAppendList(listReverse(tokens));
tokens2 := "_NO_TOKEN"::List.sortedUnique(List.sort(tokens, Util.strcmpBool), stringEqual);
// numMatches := listLength(tokens2);
cp := stringDelimitList(tokens2, ",\n ");
resTable := cp::resTable;
cp := "\nend Token;";
cp := "\n);";
resTable := cp::resTable;

resTable := listReverse(resTable);
Expand Down
7 changes: 1 addition & 6 deletions openmodelica/omcc/LexerTest.mo
Expand Up @@ -4,12 +4,7 @@ import LexerModelica;
import ParserModelica;
import ParseCodeModelica;

import Absyn;
import Util;
import RTOpts;
import System;
import Types;
import OMCCTypes;
import Flags;
import Error;

Expand All @@ -19,7 +14,7 @@ public function main
start the translation."
input list<String> inStringLst;
protected
list<OMCCTypes.Token> tokens;
list<LexerModelica.Token> tokens;
type Mcode_MCodeLst = list<Mcode.MCode>;
algorithm
_ := matchcontinue (inStringLst)
Expand Down
3 changes: 0 additions & 3 deletions openmodelica/omcc/LexerTest.mos
@@ -1,8 +1,5 @@
setCommandLineOptions({"+g=MetaModelica","+d=rml"});

loadFile("OMCCTypes.mo");
loadFile("OMCCBaseLexer.mo");

compilerSources := "../../../OMCompiler/Compiler/";

loadFiles({compilerSources + f for f in {
Expand Down
2 changes: 1 addition & 1 deletion openmodelica/omcc/Makefile
Expand Up @@ -27,7 +27,7 @@ parser-all: omcc
lexer: LexerTest_main.so
$(CC) -g -O2 -o $@ $< '-Wl,-rpath,$$ORIGIN'

LexerTest_main.so: LexerTest.mos LexerTest.mo LexerModelica.mo LexerCodeModelica.mo LexTableModelica.mo TokenModelica.mo
LexerTest_main.so: LexerTest.mos LexerTest.mo LexerModelica.mo
rm -f LexerTest_main.makefile
$(OMC) $<
$(MAKE) -f LexerTest_main.makefile
Expand Down
2 changes: 1 addition & 1 deletion openmodelica/omcc/OMCC.mo
Expand Up @@ -29,7 +29,7 @@ algorithm
tokens = LexerGenerator.buildTokens("lexer"+ parser +".l");
str = LexerGenerator.genLexer("lexer"+ parser +".c", "lexer"+ parser +".l", parser, tokens=tokens);
print("Result:" + str + "\n");
print("\nGenerated files:\n" + sum(" " + s + "Modelica.mo\n" for s in {"Lexer"}));
print("\nGenerated files:\n" + sum(" " + s + parser +".mo\n" for s in {"Lexer"}));
then ();
case args as _::_
equation
Expand Down
49 changes: 12 additions & 37 deletions openmodelica/omcc/OMCCBaseLexer.mo
@@ -1,11 +1,6 @@
encapsulated partial package OMCCBaseLexer "Implements the DFA of OMCC"
import Types;
import LexerCode;
import LexTable;
import OMCCTypes;

import System;
import List;
import Util;

import arrayGet = MetaModelica.Dangerous.arrayGetNoBoundsChecking; // Bounds checked with debug=true
import stringGet = MetaModelica.Dangerous.stringGetNoBoundsChecking;
Expand Down Expand Up @@ -34,14 +29,14 @@ replaceable partial function action
input Boolean debug;
input String fileNm;
input String fileContents;
output OMCCTypes.Token token;
output Token.Token token;
output Integer mm_startSt;
output Integer bufferRet;
end action;

function scan "Scan starts the lexical analysis, load the tables and consume the program to output the tokens"
input String fileName "input source code file";
output list<OMCCTypes.Token> tokens "return list of tokens";
output list<Token.Token> tokens "return list of tokens";
protected
String contents;
algorithm
Expand All @@ -51,7 +46,7 @@ end scan;

function scanString "Scan starts the lexical analysis, load the tables and consume the program to output the tokens"
input String fileSource "input source code file";
output list<OMCCTypes.Token> tokens "return list of tokens";
output list<Token.Token> tokens "return list of tokens";
algorithm
tokens := lex("<StringSource>",fileSource);
end scanString;
Expand All @@ -70,7 +65,7 @@ end loadSourceCode;
function lex "Scan starts the lexical analysis, load the tables and consume the program to output the tokens"
input String fileName "input source code file";
input String contents;
output list<OMCCTypes.Token> tokens "return list of tokens";
output list<Token.Token> tokens "return list of tokens";
protected
Integer startSt,numStates,i,r,cTok,cTok2,currSt,pos,sPos,ePos,linenr,contentLen,numBacktrack,buffer,lineNrStart;
list<Integer> cProg,cProg2;
Expand Down Expand Up @@ -115,23 +110,23 @@ end lex;

function consume
input Integer cp;
input list<OMCCTypes.Token> tokens;
input list<Token.Token> tokens;
input String fileContents;
input Integer startSt;
input Integer currSt,pos,sPos,ePos,linenr,inLineNrStart;
input Integer inBuffer;
input array<Integer> inStates;
input Integer inNumStates;
input String fileName;
output list<OMCCTypes.Token> resToken;
output list<Token.Token> resToken;
output Integer bkBuffer = 0;
output Integer mm_startSt;
output Integer mm_currSt,mm_pos,mm_sPos,mm_ePos,mm_linenr,lineNrStart;
output Integer buffer;
output array<Integer> states;
output Integer numStates;
protected
OMCCTypes.Token tok;
Token.Token tok;
Integer act,buffer2;
Integer c,baseCond;
algorithm
Expand Down Expand Up @@ -217,16 +212,8 @@ algorithm
end if;
buffer := buffer2;

/*
_ := match otok
local
OMCCTypes.Token tok;
case SOME(tok) algorithm print("Output token: " + OMCCTypes.printToken(tok) + "\n"); then ();
else ();
end match;
*/
resToken := match tok
case OMCCTypes.TOKEN(id=-1) then tokens;
case Token.TOKEN(id=-1) then tokens;
else tok::tokens;
end match;
if(debug) then
Expand Down Expand Up @@ -358,24 +345,12 @@ function getInfo
input Integer lineNrStart;
input Integer mm_linenr;
input String programName;
output OMCCTypes.Info info;
output SourceInfo info;
protected
Integer mm_sPos;
Integer c;
algorithm
/* for i in (List.isEmpty(buff1)==false) loop
c::buff1 := buff1;
if (c==10) then
mm_linenr := mm_linenr - 1;
mm_sPos := 0;
else
mm_sPos := mm_sPos - 1;
end if;
end while; */
info := OMCCTypes.INFO(programName,false,lineNrStart,ePos+1,mm_linenr /* flineNr */, sPos+1 /* frPos+1 */,Absyn.dummyTimeStamp);
/*if (true) then
print("\nTOKEN file:" +programName + " p(" + intString(mm_sPos) + ":" + intString(mm_linenr) + ")-(" + intString(frPos) + ":" + intString(flineNr) + ")");
end if; */
info := SOURCEINFO(programName,false,lineNrStart,ePos+1,mm_linenr /* flineNr */, sPos+1 /* frPos+1 */,Absyn.dummyTimeStamp);
end getInfo;

function printBuffer2
Expand Down Expand Up @@ -410,7 +385,7 @@ protected
Integer c;
algorithm
outList := "";
while (List.isEmpty(inList1)==false) loop
while (listEmpty(inList1)==false) loop
c::inList1 := inList1;
outList := outList + intStringChar(c);
end while;
Expand Down

0 comments on commit 2e69619

Please sign in to comment.