Skip to content

Commit

Permalink
Moved OMCC to separate repository
Browse files Browse the repository at this point in the history
Updated TestDiffAlgorithmModelica to use the new paths.
  • Loading branch information
sjoelund committed Jun 30, 2015
1 parent b895eaa commit 061bcf6
Show file tree
Hide file tree
Showing 22 changed files with 3 additions and 5,939 deletions.
155 changes: 3 additions & 152 deletions openmodelica/bootstrapping/TestDiffAlgorithmModelica.mo
@@ -1,5 +1,5 @@
function diffModelica
import LexerModelicaDiff.{Token,TokenId,tokenContent,scan,scanString};
import LexerModelicaDiff.{Token,TokenId,tokenContent,scan,scanString,filterModelicaDiff,modelicaDiffTokenEq};
import DiffAlgorithm.{Diff,diff,printActual,printDiffTerminalColor};
input String before;
input String after;
Expand All @@ -16,159 +16,10 @@ algorithm
diffs := diff(tbefore, tafter, modelicaDiffTokenEq);
diffs := filterModelicaDiff(diffs);
// Scan a second time, with comments filtered into place
after2 := printActual(diffs, LexerModelicaDiff.tokenContent);
after2 := printActual(diffs, tokenContent);
tafter2 := scanString(after2);
diffs := diff(tbefore, tafter2, modelicaDiffTokenEq);
diffs := filterModelicaDiff(diffs);
print(printDiffTerminalColor(diffs, LexerModelicaDiff.tokenContent));
print(printDiffTerminalColor(diffs, tokenContent));
print("\n\n");
end diffModelica;

function modelicaDiffTokenEq
import LexerModelicaDiff.{Token,TokenId,tokenContent};
input Token ta,tb;
output Boolean b;
protected
LexerModelicaDiff.TokenId ida,idb;
algorithm
LexerModelicaDiff.TOKEN(id=ida) := ta;
LexerModelicaDiff.TOKEN(id=idb) := tb;
if ida <> idb then
b := false;
return;
end if;
b := match ida
case TokenId.IDENT then tokenContent(ta)==tokenContent(tb);
case TokenId.UNSIGNED_INTEGER then tokenContent(ta)==tokenContent(tb);
case TokenId.UNSIGNED_REAL
then stringReal(tokenContent(ta))==stringReal(tokenContent(tb));
case TokenId.BLOCK_COMMENT
then valueEq(blockCommentCanonical(ta),blockCommentCanonical(tb));
case TokenId.LINE_COMMENT then tokenContent(ta)==tokenContent(tb);
case TokenId.STRING then tokenContent(ta)==tokenContent(tb);
case TokenId.WHITESPACE then true; // tokenContent(ta)==tokenContent(tb);
else true;
end match;
end modelicaDiffTokenEq;

function filterModelicaDiff
import LexerModelicaDiff.{Token,TokenId,tokenContent,TOKEN};
import DiffAlgorithm.Diff;
input list<tuple<Diff, list<Token>>> diffs;
output list<tuple<Diff, list<Token>>> odiffs;
protected
list<String> addedLineComments, removedLineComments;
list<list<String>> addedBlockComments, removedBlockComments;
list<tuple<Diff, Token>> simpleDiff;
algorithm
// No changes are easy
_ := match diffs
case {(Diff.Equal,_)}
algorithm
odiffs := diffs;
return;
then ();
else ();
end match;

odiffs := listReverse(match e
local
list<Token> ts;
case (Diff.Delete,ts as {TOKEN(id=TokenId.WHITESPACE)}) then (Diff.Equal,ts);
else e;
end match

for e guard(
match e
// Single addition of whitespace, not followed by another addition
// is suspected garbage added by OMC.
case (Diff.Add,{TOKEN(id=TokenId.WHITESPACE)}) then false;
case (_,{}) then false;
else true;
end match
) in diffs);

// Convert from multiple additions per item to one per item
// Costs more memory, but is easier to transform
simpleDiff := listAppend(
match e
local
list<Token> ts;
case (Diff.Add,ts) then list((Diff.Add,t) for t in ts);
case (Diff.Equal,ts) then list((Diff.Equal,t) for t in ts);
case (Diff.Delete,ts) then list((Diff.Delete,t) for t in ts);
end match
for e in odiffs);

addedLineComments := list(tokenContent(tuple22(e)) for e guard Diff.Add==tuple21(e) and isLineComment(tuple22(e)) in simpleDiff);
removedLineComments := list(tokenContent(tuple22(e)) for e guard Diff.Delete==tuple21(e) and isLineComment(tuple22(e)) in simpleDiff);

addedBlockComments := list(blockCommentCanonical(tuple22(e)) for e guard Diff.Add==tuple21(e) and isBlockComment(tuple22(e)) in simpleDiff);
removedBlockComments := list(blockCommentCanonical(tuple22(e)) for e guard Diff.Delete==tuple21(e) and isBlockComment(tuple22(e)) in simpleDiff);

simpleDiff := list(
match e
local
Token t;
case (Diff.Delete,t as TOKEN(id=TokenId.LINE_COMMENT)) then (if listMember(tokenContent(t), addedLineComments) then (Diff.Equal,t) else e);
case (Diff.Delete,t as TOKEN(id=TokenId.BLOCK_COMMENT)) then (if listMember(blockCommentCanonical(t), addedBlockComments) then (Diff.Equal,t) else e);
else e;
end match
for e guard(
match e
local
Token t;
case (Diff.Add,t as TOKEN(id=TokenId.LINE_COMMENT)) then not listMember(tokenContent(t), removedLineComments);
case (Diff.Add,t as TOKEN(id=TokenId.BLOCK_COMMENT)) then not listMember(blockCommentCanonical(t), removedBlockComments);
else true;
end match
) in simpleDiff);

odiffs := list(
match e
local
Diff d;
Token t;
case (d,t) then (d,{t});
end match
for e in simpleDiff);
end filterModelicaDiff;

function isBlockComment
import LexerModelicaDiff.{Token,TokenId,TOKEN};
input Token t;
output Boolean b;
algorithm
b := match t case TOKEN(id=TokenId.BLOCK_COMMENT) then true; else false; end match;
end isBlockComment;

function isLineComment
import LexerModelicaDiff.{Token,TokenId,TOKEN};
input Token t;
output Boolean b;
algorithm
b := match t case TOKEN(id=TokenId.LINE_COMMENT) then true; else false; end match;
end isLineComment;

function tuple21<A,B>
input tuple<A,B> t;
output A a;
algorithm
(a,_) := t;
end tuple21;

function tuple22<A,B>
input tuple<A,B> t;
output B b;
algorithm
(_,b) := t;
end tuple22;

function blockCommentCanonical
import LexerModelicaDiff.{Token,tokenContent};
input Token t;
output list<String> lines;
algorithm
// Canonical representation trims whitespace from each line
lines := list(System.trim(s) for s in System.strtok(tokenContent(t),"\n"));
end blockCommentCanonical;
11 changes: 0 additions & 11 deletions openmodelica/omcc/.gitignore

This file was deleted.

33 changes: 0 additions & 33 deletions openmodelica/omcc/GenerateParser.mos

This file was deleted.

0 comments on commit 061bcf6

Please sign in to comment.