Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a sketch of a pretty-printer and a stub of a normaliser
- Loading branch information
1 parent
e06486b
commit 66065c5
Showing
5 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
//{ | ||
// | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>"; |