Last active
August 29, 2015 14:16
-
-
Save lrlucena/6eb624ceeab8a372c9dd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
grammar linguagem; | |
pagina: (Texto | formulario)*; | |
formulario: 'form' label 'para' Destino campo* submit; | |
campo: label ':' Tipo # simples | |
| 'select' label ':' grupo # multipla | |
| 'radio' label ':' grupo # radio | |
| 'checkbox' label # checkbox | |
; | |
submit: 'enviar' Texto; | |
grupo: '{' Texto (',' Texto)* '}'; | |
label: ID (Texto)?; | |
Tipo: 'texto'|'numero'|'data'|'email'; | |
Destino: '"http://' .*? '"'; | |
ID: [A-Za-z][A-Za-z0-9]*; | |
Texto: '"' .*? '"'; | |
Pular: [ \n\r] -> skip; |
This file contains hidden or 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
package br.edu.ifrn.linguagem; | |
import org.antlr.v4.runtime.ParserRuleContext; | |
import org.antlr.v4.runtime.misc.NotNull; | |
import org.antlr.v4.runtime.tree.ParseTree; | |
import org.antlr.v4.runtime.tree.ParseTreeProperty; | |
import org.antlr.v4.runtime.tree.TerminalNode; | |
public class MinhaLinguagem extends linguagemBaseListener { | |
private final ParseTreeProperty<String> values = new ParseTreeProperty<String>(); | |
private String saida = ""; | |
private void setValue(ParseTree node, String value) { | |
values.put(node, value); | |
} | |
private String getValue(ParseTree node) { | |
return values.get(node); | |
} | |
public String getSaida() { | |
return saida; | |
} | |
@Override | |
public void exitPagina(@NotNull linguagemParser.PaginaContext ctx) { | |
} | |
@Override | |
public void exitFormulario(@NotNull linguagemParser.FormularioContext ctx) { | |
} | |
@Override | |
public void exitMultipla(@NotNull linguagemParser.MultiplaContext ctx) { | |
} | |
@Override | |
public void exitSubmit(@NotNull linguagemParser.SubmitContext ctx) { | |
} | |
@Override | |
public void exitCheckbox(@NotNull linguagemParser.CheckboxContext ctx) { | |
} | |
@Override | |
public void exitGrupo(@NotNull linguagemParser.GrupoContext ctx) { | |
} | |
@Override | |
public void exitLabel(@NotNull linguagemParser.LabelContext ctx) { | |
} | |
@Override | |
public void exitSimples(@NotNull linguagemParser.SimplesContext ctx) { | |
} | |
@Override | |
public void exitRadio(@NotNull linguagemParser.RadioContext ctx) { | |
} | |
@Override | |
public void exitEveryRule(@NotNull ParserRuleContext ctx) { | |
} | |
@Override | |
public void visitTerminal(@NotNull TerminalNode node) { | |
} | |
} |
This file contains hidden or 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
package br.edu.ifrn.linguagem; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import org.antlr.v4.runtime.ANTLRInputStream; | |
import org.antlr.v4.runtime.CommonTokenStream; | |
import org.antlr.v4.runtime.tree.ParseTree; | |
import org.antlr.v4.runtime.tree.ParseTreeWalker; | |
public class Principal { | |
public static void main(String[] args) { | |
if (args.length == 0) { | |
System.out.println("Uso: potigol [arquivo.poti]\n"); | |
return; | |
} | |
try { | |
String arq = args[0]; | |
final MinhaLinguagem listener = getListner(arq); | |
final String saida = listener.getSaida(); | |
System.out.println(saida); | |
} catch (IOException e) { | |
System.out.println("Erro: Arquivo " + args[0] + " não encontrado."); | |
} | |
} | |
private static MinhaLinguagem getListner(final String arq) throws IOException { | |
String r = lerArquivo(arq); | |
final ANTLRInputStream input = new ANTLRInputStream(r); | |
final linguagemLexer lexer = new linguagemLexer(input); | |
final CommonTokenStream tokens = new CommonTokenStream(lexer); | |
final linguagemParser parser = new linguagemParser(tokens); | |
final ParseTree tree = parser.pagina(); | |
final ParseTreeWalker walker = new ParseTreeWalker(); | |
final MinhaLinguagem listener = new MinhaLinguagem(); | |
try { | |
walker.walk(listener, tree); | |
} catch (Exception e) { | |
} | |
return listener; | |
} | |
private static String lerArquivo(String arq) throws IOException { | |
Path path = Paths.get(arq); | |
java.util.List<String> linhas = Files.readAllLines(path, | |
StandardCharsets.UTF_8); | |
StringBuffer s = new StringBuffer(); | |
for (String linha : linhas) { | |
s.append(linha + "\n"); | |
} | |
return s.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment