-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrechos.cs
104 lines (87 loc) · 2.67 KB
/
Trechos.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Trechos
{
public Fonte Fonte { get; set; }
public Trecho[] Conteudo { get; set; }
public int PosAtual { get; set; }
public bool FimDaLinha => Atual.Tipo == TipoTrecho.FimDaLinha;
public bool FimDoArquivo { get; set; }
public Trechos(Fonte fonte, Trecho[] trechos)
{
Fonte = fonte;
Conteudo = trechos;
PosAtual = -1;
Proximo();
}
public bool ProximaLinha()
{
Trechos tmp = Fonte.LeiaLinha();
PosAtual = -1;
Conteudo = tmp.Conteudo;
FimDoArquivo = tmp.FimDoArquivo;
Proximo();
return !FimDoArquivo;
}
public Trecho Atual => PosAtual < Conteudo.Length ? Conteudo[PosAtual] : Conteudo.Last();
public Trecho Anterior => Conteudo[PosAtual-1];
public bool EhProximoTipo(TipoTrecho tipo)
{
if((PosAtual + 1) >= Conteudo.Length) return false;
return Conteudo[PosAtual + 1].Tipo == tipo;
}
public bool EhIdentificador() => Atual.Tipo == TipoTrecho.Id;
public bool EhIdentificador(string id) => (EhIdentificador() && Atual.Conteudo.ToLower() == id.ToLower());
public bool EhOpMatematica() => Atual.Tipo == TipoTrecho.OperacaoMatematica;
public bool EhOpMatematica(string id) => (EhOpMatematica() && Atual.Conteudo.ToLower() == id.ToLower());
public bool EhOpLogica() => Atual.Tipo == TipoTrecho.OperacaoLogica;
public bool EhOpLogica(string id) => (EhOpLogica() && Atual.Conteudo.ToLower() == id.ToLower());
public bool EhTipo(TipoTrecho tipo) => Atual.Tipo == tipo;
public bool Proximo()
{
PosAtual++;
return !FimDaLinha;
}
public void Erro(string erro)
{
throw new Erro(Atual, erro);
}
public void ExigeProximoFimDaLinha(string erro)
{
Proximo();
if(!FimDaLinha) throw new Erro(Atual, erro);
}
public void ExigeFimDaLinha(string erro)
{
if(!FimDaLinha) throw new Erro(Atual, erro);
}
public void ExigeTipo(TipoTrecho tipo, string erro)
{
if(!EhTipo(tipo)) throw new Erro(Atual, erro);
}
public void ExigeProximo(string erro)
{
if(!Proximo())
{
PosAtual --;
throw new Erro(Atual, erro);
}
}
public void ExigeId(string erro)
{
if(!EhIdentificador())
throw new Erro(Atual, erro);
}
public void ExigeId(string id, string erro)
{
if(!EhIdentificador(id))
throw new Erro(Atual, erro);
}
public void ExigeProximaLinha(Trecho ultimo, string erro)
{
if(!ProximaLinha())
throw new Erro(ultimo, erro);
}
public override string ToString()
{
return Atual.ToString();
}
}