Skip to content

Commit

Permalink
a sketch of a pretty-printer and a stub of a normaliser
Browse files Browse the repository at this point in the history
  • Loading branch information
grammarware committed Dec 23, 2012
1 parent e06486b commit 66065c5
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
7 changes: 7 additions & 0 deletions rascal/src/Rascalware.rsc
@@ -0,0 +1,7 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module Rascalware

public str folds(str(&T) f, list[&T] ss) = ("" | it + f(s) | &T s <- ss);
public list[&T] mapl(&T(&D) f, list[&D] xs) = [f(x) | &D x <- xs];
//public list[&T] mapc2a(&T(&D) f, {&D &L}+ xs) = [f(x) | &D x <- xs];
//public list[&T] mapc2a(&T(&D) f, &D+ xs) = [f(x) | &D x <- xs];
21 changes: 21 additions & 0 deletions rascal/src/io/bibtex/Normal.rsc
@@ -0,0 +1,21 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module io::bibtex::Normal

import io::bibtex::Unparser;
import io::bibtex::Parser;
import IO;

public void main()
{
println(bib2str([normalise(e) | e <- loc2bib(|home:///workspace/bibtex/list.bib|)]));
}

BibEntry normalise(BibEntry e)
{
return e;
}

//BibEntry normalise(BibEntry e)
//{
//
//}
57 changes: 53 additions & 4 deletions rascal/src/io/bibtex/Parser.rsc
@@ -1,24 +1,73 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module io::bibtex::Parser

//import Rascalware;
import io::bibtex::Syntax;
import ParseTree;
import String;
import List; // size
import IO;

alias BibLib = list[BibEntry];
alias BibEntry = tuple[str kind, str name, map[str,str] attrs];
alias BibAttrs = map[str,BibString];
alias BibEntry = tuple[str kind, str name, BibAttrs attrs];
data BibString
= raw(str s)
| quoted(BibString bs)
| bracketed(BibString bs)
| bibseq(list[BibString])
;

public BibLib loc2bib(loc l) = str2bib(readFile(l));
BibLib str2bib(str s) = library2list(parse(#BibLibrary,trim(s)));

//BibLib library2list(BibLibrary b) = mapc2a(mapEntry,b.es);
BibLib library2list(BibLibrary b) = [mapEntry(e) | OneBibEntry e <- b.es];

BibEntry mapEntry(OneBibEntry e) = <"<e.kind.name>","<e.name>",mapKVs(e.pairs)>;

map[str,str] mapKVs({BibPair ","}+ ps) = ("<p.key>":"<p.val>" | BibPair p <- ps);
BibAttrs mapKVs({BibPair ","}+ ps) = ("<p.key>":mapStr(p.val) | BibPair p <- ps);

BibString mapStr((BibValue)`<BibValueQ v>`) = quoted(mapStrQ(v));
BibString mapStr((BibValue)`<BibValueC v>`) = bracketed(mapStrC(v));
default BibString mapStr(BibValue v) = raw("<v>");

BibString mapStrQ(BibValueQ v) = normalise([mapQEl(e) | BQElement e <- v.es]);

BibString mapQEl((BQElement)`<BibValueC c>`) = bracketed(mapStrC(c));
default BibString mapQEl(BQElement e) = raw("<e>");

BibString mapStrC(BibValueC v) = normalise([mapCEl(e) | BCElement e <- v.es]);

BibString mapCEl((BCElement)`<BibValueC c>`) = bracketed(mapStrC(c));
default BibString mapCEl(BCElement e) = raw("<e>");

BibString normalise(list[BibString] bs)
{
bs2 = innermost visit(bs)
{
case [*L1,raw(str s1),raw(str s2),*L2] => [*L1,raw(s1+s2),*L2]
};
if (size(bs2)==1)
return bs2[0];
else
return bibseq(bs2);
}


public void main()
{
loc2bib(|home:///workspace/zaytsev.bib|);
}
iprintln(loc2bib(|home:///workspace/bibtex/icse2010.bib|)[0]);
//loc2bib(|home:///workspace/zaytsev.bib|);
}

// TODO will be useful later for checking unparser completeness
public void do()
{
list[str] allkeys = [];
for(BibEntry b <-loc2bib(|home:///workspace/bibtex/icse2010.bib|), str x <- b.attrs)
if (x notin allkeys)
allkeys += x;
iprintln(allkeys);
}

4 changes: 2 additions & 2 deletions rascal/src/io/bibtex/Syntax.rsc
Expand Up @@ -16,10 +16,10 @@ lexical BibValue
| [a-zA-Z0-9]+ !>> [a-zA-Z0-9]
;

lexical BibValueQ = [\"] BQElement* [\"] ;
lexical BibValueQ = [\"] BQElement* es [\"] ;
lexical BQElement = ![\"\\{}] | [\\] [\"\'`&$%a-zA-Z] | BibValueC;

lexical BibValueC = [{] BCElement* [}];
lexical BibValueC = [{] BCElement* es [}];
lexical BCElement = BibValueC | ![{}];

start syntax BibLibrary = OneBibEntry+ es;
25 changes: 25 additions & 0 deletions rascal/src/io/bibtex/Unparser.rsc
@@ -0,0 +1,25 @@
@contributor{Vadim Zaytsev - vadim@grammarware.net - SWAT, CWI}
module io::bibtex::Unparser

import io::bibtex::Parser;
import Rascalware;

// TODO: check completeness, sort appropriately
list[str] good =
["author","title","booktitle","year",
"url","doi","numpages","publisher","series","acmid",
"isbn","location","address","pages","keywords"];

public str bib2str(BibLib bl) = folds(bib2str,bl);

str bib2str(BibEntry be) =
"@<be.kind>{<be.name>,
'<for(k <- good, k in be.attrs){>\t<k> = <pp(be.attrs[k])>,
'<}>}
'";

str pp(quoted(BibString s)) = "\"<pp(s)>\"";
str pp(bracketed(BibString s)) = "{<pp(s)>}";
str pp(raw(str s)) = s;
str pp(bibseq(list[BibString] ss)) = folds(pp,ss);
default str pp(BibString s) = "<s>";

0 comments on commit 66065c5

Please sign in to comment.