adjust misc for changes in meta
[sbp.git] / src / edu / berkeley / sbp / misc / HaskellHelper.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp.misc;
4
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.misc.*;
7 import edu.berkeley.sbp.meta.*;
8 import edu.berkeley.sbp.chr.*;
9 import edu.berkeley.sbp.tib.*;
10 import java.io.*;
11
12 public class HaskellHelper {
13
14     public static void main(String[] argv) throws Throwable {
15         help(argv[0], argv[1]);
16     }
17     public static boolean isNull(Object o) { return o==null; }
18     public static Tree help0(String grammarFile, String targetFile) throws Throwable {
19         try {
20             Tree<String> res = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream(grammarFile)).expand1();
21             Union meta = Grammar.create(res, "s" /*,
22                                         new Grammar.Bindings() {
23                                             public Sequence createSequence(Production p) {
24                                                 Element[] els = p.elements;
25                                                 if (p.tag != null)
26                                                     return Sequence.create(p.tag, p.elements, p.drops, false);
27                                                 int idx = -1;
28                                                 for(int i=0; i<els.length; i++)
29                                                     if (!p.drops[i])
30                                                         if (idx==-1) idx = i;
31                                                         else return Sequence.create(p.nonTerminal, p.elements, p.drops, false);
32                                                 if (idx != -1) return Sequence.create(els, idx);
33                                                 else           return Sequence.create(els, "");
34                                             }
35                                             
36                                             }*/);
37             System.out.println();
38             System.out.println();
39             CharInput input = new CharInput(new FileInputStream(targetFile), "", true);
40             //Input input = new Tib(new FileInputStream(targetFile));
41             Tree ret = new CharParser(meta).parse(input).expand1();
42             if (ret==null) throw new NullPointerException("CharParser returned null");
43             return ret;
44         } catch (Throwable e) {
45             e.printStackTrace();
46             throw e;
47         }
48     }
49     public static Tree help(String grammarFile, String targetFile) throws Throwable {
50         try {
51             Tree<String> res = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream(grammarFile)).expand1();
52             Union meta = Grammar.create(res, "s" /*,
53                                         new Grammar.Bindings() {
54                                             public Sequence createSequence(Production p) {
55                                                 Element[] els = p.elements;
56                                                 if (p.tag != null && !"".equals(p.tag))
57                                                     return Sequence.create(p.tag, p.elements, p.drops, false);
58                                                 int idx = -1;
59                                                 for(int i=0; i<els.length; i++)
60                                                     if (!p.drops[i])
61                                                         if (idx==-1) idx = i;
62                                                         else return Sequence.create(p.nonTerminal, p.elements, p.drops, false);
63                                                 if (idx != -1) return Sequence.create(els, idx);
64                                                 else           return Sequence.create(els, "");
65                                             }
66                                             
67                                             }*/);
68             System.out.println();
69             System.out.println();
70             //CharInput input = new CharInput(new FileInputStream(targetFile), "", true);
71             Input input = new Tib(new FileInputStream(targetFile));
72             Tree ret = new CharParser(meta).parse(input).expand1();
73             if (ret==null) throw new NullPointerException("CharParser returned null");
74             return ret;
75         } catch (Throwable e) {
76             e.printStackTrace();
77             throw e;
78         }
79     }
80
81
82 }