Skip to main content

oak/front/
ast.rs

1// Copyright 2014 Pierre Talbot (IRCAM)
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7//     http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub use ast::*;
16
17pub struct FGrammar
18{
19  pub start_span: Span,
20  pub rules: Vec<Rule>,
21  pub exprs: Vec<Expression>,
22  pub exprs_info: Vec<FExpressionInfo>,
23  pub rust_items: Vec<syn::Item>,
24  pub attributes: Vec<syn::Attribute>
25}
26
27impl FGrammar
28{
29  pub fn new(start_span: Span) -> FGrammar {
30    FGrammar {
31      start_span,
32      rules: vec![],
33      exprs: vec![],
34      exprs_info: vec![],
35      rust_items: vec![],
36      attributes: vec![]
37    }
38  }
39
40  pub fn alloc_expr(&mut self, span: Span, expr: Expression) -> usize {
41    let expr_idx = self.exprs.len();
42    self.exprs.push(expr);
43    self.exprs_info.push(FExpressionInfo::spanned(span));
44    expr_idx
45  }
46
47  pub fn push_rule(&mut self, name: Ident, def: usize) {
48    self.rules.push(Rule::new(name, def));
49  }
50
51  pub fn push_attrs(&mut self, attrs: Vec<syn::Attribute>) {
52    self.attributes.extend(attrs.into_iter());
53  }
54
55  pub fn push_rust_item(&mut self, ritem: syn::Item) {
56    self.rust_items.push(ritem);
57  }
58}
59
60// Implicitly typed expression.
61#[derive(Clone)]
62pub struct FExpressionInfo
63{
64  pub span: Span
65}
66
67impl FExpressionInfo
68{
69  fn spanned(span: Span) -> FExpressionInfo {
70    FExpressionInfo { span }
71  }
72}
73
74impl Spanned for FExpressionInfo {
75  fn span(&self) -> Span {
76    self.span
77  }
78}