forked from jasder/antlr
couldn't get Horstmann's routine to do EPS not PS so had to backtrack.
This commit is contained in:
parent
22bb947019
commit
f220212a95
|
@ -268,17 +268,17 @@ public class RuleContext implements ParseTree.RuleNode {
|
|||
public void save(Parser parser, String fileName)
|
||||
throws IOException, PrintException
|
||||
{
|
||||
TreeViewer viewer = new TreeViewer(parser, this);
|
||||
viewer.save(fileName);
|
||||
// Trees.writePS(this, parser, fileName); // parrt routine
|
||||
// TreeViewer viewer = new TreeViewer(parser, this);
|
||||
// viewer.save(fileName);
|
||||
Trees.writePS(this, parser, fileName); // parrt routine
|
||||
}
|
||||
|
||||
// public void save(Parser parser, String fileName,
|
||||
// String fontName, int fontSize)
|
||||
// throws IOException
|
||||
// {
|
||||
// Trees.writePS(this, parser, fileName, fontName, fontSize);
|
||||
// }
|
||||
public void save(Parser parser, String fileName,
|
||||
String fontName, int fontSize)
|
||||
throws IOException
|
||||
{
|
||||
Trees.writePS(this, parser, fileName, fontName, fontSize);
|
||||
}
|
||||
|
||||
/** Print out a whole tree, not just a node, in LISP format
|
||||
* (root child1 .. childN). Print just a node if this is a leaf.
|
||||
|
|
|
@ -46,6 +46,7 @@ import java.awt.print.Printable;
|
|||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class GraphicsSupport {
|
||||
/**
|
||||
|
@ -79,11 +80,12 @@ public class GraphicsSupport {
|
|||
public static void saveImage(final JComponent comp, String fileName)
|
||||
throws IOException, PrintException
|
||||
{
|
||||
if (fileName.endsWith(".ps") || fileName.endsWith(".eps")) {
|
||||
if (fileName.endsWith(".ps") || fileName.endsWith(".eps") ) {
|
||||
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
|
||||
String mimeType = "application/postscript";
|
||||
StreamPrintServiceFactory[] factories =
|
||||
StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, mimeType);
|
||||
System.out.println(Arrays.toString(factories));
|
||||
FileOutputStream out = new FileOutputStream(fileName);
|
||||
if (factories.length > 0) {
|
||||
PrintService service = factories[0].getPrintService(out);
|
||||
|
@ -92,13 +94,17 @@ public class GraphicsSupport {
|
|||
public int print(Graphics g, PageFormat pf, int page) {
|
||||
if (page >= 1) return Printable.NO_SUCH_PAGE;
|
||||
else {
|
||||
double sf1 = pf.getImageableWidth() / (comp.getWidth() + 1);
|
||||
double sf2 = pf.getImageableHeight() / (comp.getHeight() + 1);
|
||||
double s = Math.min(sf1, sf2);
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
g2.translate((pf.getWidth() - pf.getImageableWidth()) / 2,
|
||||
(pf.getHeight() - pf.getImageableHeight()) / 2);
|
||||
g2.scale(s, s);
|
||||
if ( comp.getWidth() > pf.getImageableWidth() ||
|
||||
comp.getHeight() > pf.getImageableHeight() )
|
||||
{
|
||||
double sf1 = pf.getImageableWidth() / (comp.getWidth() + 1);
|
||||
double sf2 = pf.getImageableHeight() / (comp.getHeight() + 1);
|
||||
double s = Math.min(sf1, sf2);
|
||||
g2.scale(s, s);
|
||||
}
|
||||
|
||||
comp.paint(g);
|
||||
return Printable.PAGE_EXISTS;
|
||||
|
@ -110,17 +116,20 @@ public class GraphicsSupport {
|
|||
job.print(doc, attributes);
|
||||
}
|
||||
} else {
|
||||
// parrt: doesn't seem to do .pdf. 0-length file generated
|
||||
// parrt: works with [image/jpeg, image/png, image/x-png, image/vnd.wap.wbmp, image/bmp, image/gif]
|
||||
Rectangle rect = comp.getBounds();
|
||||
BufferedImage image = new BufferedImage(rect.width, rect.height,
|
||||
BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g = (Graphics2D) image.getGraphics();
|
||||
g.setColor(Color.WHITE);
|
||||
g.fill(rect);
|
||||
g.setColor(Color.BLACK);
|
||||
// g.setColor(Color.BLACK);
|
||||
comp.paint(g);
|
||||
String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
|
||||
ImageIO.write(image, extension, new File(fileName));
|
||||
boolean result = ImageIO.write(image, extension, new File(fileName));
|
||||
if ( !result ) {
|
||||
System.err.println("Now imager for " + extension);
|
||||
}
|
||||
g.dispose();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,71 +1,8 @@
|
|||
parser grammar T;
|
||||
grammar T;
|
||||
|
||||
options {tokenVocab=CharVocab;}
|
||||
s : ID b ;
|
||||
b : INT ;
|
||||
|
||||
@header {
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
}
|
||||
|
||||
@members {
|
||||
public static Set<String> keywords = new HashSet<String>() {{
|
||||
add("void");
|
||||
add("int");
|
||||
add("return");
|
||||
}};
|
||||
boolean notLetterOrDigit(int la) {
|
||||
return !Character.isLetterOrDigit(_input.LA(la));
|
||||
}
|
||||
}
|
||||
|
||||
// parser rules
|
||||
|
||||
prog: ws? (var|func)+ EOF ;
|
||||
|
||||
type: kint | kvoid | id ;
|
||||
|
||||
var : type id semi ;
|
||||
|
||||
func: type id lp args rp body ;
|
||||
|
||||
args: arg (',' arg)* ;
|
||||
|
||||
arg : type id ;
|
||||
|
||||
body: '{' stat+ '}' ;
|
||||
|
||||
stat: kreturn e semi
|
||||
| id eq e semi
|
||||
| id colon
|
||||
;
|
||||
|
||||
e : integer | id ;
|
||||
|
||||
// lexical rules
|
||||
|
||||
id : letter+ {!keywords.contains($text)}? ws? ;
|
||||
|
||||
lp : '(' ws? ;
|
||||
rp : ')' ws? ;
|
||||
eq : '=' ws? ;
|
||||
colon: ':' ws? ;
|
||||
semi: ';' ws? ;
|
||||
|
||||
kint: {notLetterOrDigit(4)}? 'i' 'n' 't' ;
|
||||
kvoid: {notLetterOrDigit(5)}? 'v' 'o' 'i' 'd' ;
|
||||
kreturn: {notLetterOrDigit(7)}? 'r' 'e' 't' 'u' 'r' 'n' ;
|
||||
|
||||
integer : digit+ ws? ;
|
||||
|
||||
digit
|
||||
: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
|
||||
;
|
||||
|
||||
letter
|
||||
: 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j'
|
||||
| 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't'
|
||||
| 'u' | 'v' | 'w' | 'x' | 'y' | 'z'
|
||||
;
|
||||
|
||||
ws : (' ' | '\t' | '\n' | comment)+ ;
|
||||
comment : '/' '/' ~('\r'|'\n')* '\r' '\n' ;
|
||||
ID : [a-zA-Z]+ ;
|
||||
INT : [0-9]+ ;
|
||||
WS : [ \t\n\r]+ -> skip ;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CommonTokenFactory;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.UnbufferedCharStream;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
|
@ -22,7 +23,8 @@ public class TestT {
|
|||
CommonTokenStream tokens = new CommonTokenStream(lex);
|
||||
TParser parser = new TParser(tokens);
|
||||
|
||||
parser.setBuildParseTree(true);
|
||||
parser.s();
|
||||
ParserRuleContext tree = parser.s();
|
||||
tree.save(parser, "/tmp/t.ps");
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue