Gramática — Declarações
9 produções: ação, record, enum, blueprint, trait, type. Cada exemplo é aceito pelo parser.
Declarações dão nome a algo que dura: uma ação, um tipo, um molde de objeto. Duas de topo com o mesmo nome no mesmo arquivo viram aviso — a primeira não tem como ser alcançada.
| Produção | Nós que ela produz |
|---|---|
action | ActionDeclaration, YieldStatement |
stream | EmitStatement |
record | RecordDeclaration |
enum | EnumDeclaration |
blueprint | BlueprintDeclaration |
trait | TraitDeclaration |
type | TypeDeclaration |
shadow_static | ShadowDeclaration, StaticDeclaration |
root | BlueprintDeclaration |
action#
text
acao = [ "async" | "stream" ] "action" nome [ genericos ]
"(" [ parametros ] ")" [ "->" tipo ] ":" bloco ;
parametro = nome [ ":" tipo ] [ ":=" expressao ] ;dataforge
action somar(a: Integer, b := 1) -> Integer:
yield a + bstream#
text
produzir = "emit" expressao ;dataforge
stream action nat():
n := 0
persist yes:
emit n
n += 1record#
text
record = "record" nome [ genericos ] ":" NEWLINE INDENT
{ campo | acao } DEDENT ;
campo = nome ":" tipo [ ":=" expressao ] NEWLINE ;dataforge
record Ponto:
x: Integer
y: Integerenum#
text
enum = "enum" nome ":" NEWLINE INDENT { membro | acao } DEDENT ;
membro = nome [ ":=" expressao ] NEWLINE ;dataforge
enum Cor:
Verde
Azul := "a"blueprint#
text
blueprint = { modificador } "blueprint" nome [ genericos ] [ "(" campos ")" ]
[ "extends" nome ] [ "with" nome { "," nome } ] ":" bloco ;dataforge
blueprint Forma:
action area():
yield 0
blueprint Quadrado(lado) extends Forma:
action area():
yield self.lado ** 2trait#
text
trait = "trait" nome ":" NEWLINE INDENT { assinatura | acao } DEDENT ;dataforge
trait Mede:
action medir()type#
text
tipo_nomeado = [ "opaque" ] "type" nome [ genericos ] ":=" tipo
[ "where" expressao ] ;dataforge
type Id := Integer
type Positivo := Integer where valor bigger 0
opaque type Cpf := String where len(valor) is 11shadow_static#
text
sombra = "shadow" nome ":=" expressao ;
estatico = "static" nome ":=" expressao ;dataforge
x := 1
action f():
shadow x := 99
yield x
blueprint Config:
static padrao := "claro"root#
text
mae = "root" "." nome "(" [ argumentos ] ")" ;dataforge
blueprint Base:
action nome():
yield "base"
blueprint Filha extends Base:
action nome():
yield root.nome() + "+filha"Estas produções saem de dataforge/gramatica.py. No terminal: dataforge gramatica declaracoes. Volte para a gramática.