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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#![feature(proc_macro_diagnostic)]
#![feature(decl_macro)]

mod shared_array;
use shared_array::*;

mod shared_map;
use shared_map::*;

mod symbol;
use symbol::*;

mod tree_semantics;
use tree_semantics::*;

mod processing;
use processing::*;

// use std::iter::FromIterator;
use proc_macro::TokenStream;
// use proc_macro2::Span;
use quote::{quote, ToTokens};
// use quote::{quote, quote_spanned};
use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::token::{Brace, Comma};
// use syn::spanned::Spanned;
use syn::{parse_macro_input, Ident, Token, Path, Visibility, Attribute, Type, Expr, Generics, FnArg, Stmt, braced, WhereClause, parenthesized};

use std::cell::RefCell;
use std::collections::HashMap;
use std::hash::Hash;
use std::ops::Deref;
use std::rc::{Rc, Weak};
use by_address::ByAddress;

/// Field name used for holding an enumeration of submeanings.
const DATA_VARIANT_FIELD: &'static str = "__variant";

/// Prefix used for enumerations of submeanings.
const DATA_VARIANT_PREFIX: &'static str = "__variant_";

/// Variant name used for indicating that no submeaning is instantiated.
const DATA_VARIANT_NO_SUBMEANING: &'static str = "__NoSubmeaning";

struct MeaningTree {
    arena_type_name: proc_macro2::TokenStream,
    meanings: Vec<Rc<Meaning>>,
}

struct Meaning {
    attributes: Vec<Attribute>,
    visibility: Visibility,
    name: Ident,
    inherits: Option<Ident>,
    fields: Vec<Rc<MeaningField>>,
    constructor: Option<MeaningConstructor>,
    methods: Vec<Rc<MeaningMethod>>,
}

struct MeaningField {
    is_ref: bool,
    name: Ident,
    type_annotation: Type,
    default_value: Expr,
}

enum MeaningMethodOrConstructor {
    Method(MeaningMethod),
    Constructor(MeaningConstructor),
}

struct MeaningConstructor {
    attributes: Vec<Attribute>,
    visibility: Visibility,
    generics: Generics,
    inputs: Punctuated<FnArg, Comma>,
    super_arguments: Punctuated<Expr, Comma>,
    statements: Vec<Stmt>,
}

struct MeaningMethod {
    attributes: RefCell<Vec<Attribute>>,
    visibility: Visibility,
    is_override: bool,
    name: Ident,
    generics: Generics,
    inputs: Punctuated<FnArg, Comma>,
    statements: Brace,
}

impl Parse for MeaningTree {
    fn parse(input: ParseStream) -> Result<Self> {
        let arena_type_name = parse_meaning_arena_type_name(input)?.to_token_stream();
        let mut meanings = vec![];
        while !input.is_empty() {
            meanings.push(Rc::new(input.parse::<Meaning>()?));
        }
        Ok(Self {
            arena_type_name,
            meanings,
        })
    }
}

impl Parse for Meaning {
    fn parse(input: ParseStream) -> Result<Self> {
        let attributes = Attribute::parse_outer(input)?;
        let visibility = input.parse::<Visibility>()?;
 
        input.parse::<Token![struct]>()?;
 
        let name = input.parse::<Ident>()?;
        let name_str = name.to_string();

        // Inherits
        let mut inherits: Option<Ident> = None;
        if input.peek(Token![:]) {
            input.parse::<Token![:]>();
            inherits = Some(input.parse::<Ident>()?);
        }

        let mut fields: Vec<Rc<MeaningField>> = vec![];
        let mut constructor: Option<MeaningConstructor> = None;
        let mut methods: Vec<Rc<MeaningMethod>> = vec![];
        let braced_content;
        let _ = braced!(braced_content in input);

        while !braced_content.is_empty() {
            if input.peek(Token![let]) {
                fields.push(Rc::new(parse_meaning_field(input)?));
            } else {
                match parse_meaning_method(input, &name_str)? {
                    MeaningMethodOrConstructor::Constructor(ctor) => {
                        constructor = Some(ctor);
                    },
                    MeaningMethodOrConstructor::Method(m) => {
                        methods.push(Rc::new(m));
                    },
                }
            }
        }

        Ok(Self {
            attributes,
            visibility,
            name,
            inherits,
            fields,
            constructor,
            methods,
        })
    }
}

fn parse_meaning_field(input: ParseStream) -> Result<MeaningField> {
    input.parse::<Token![let]>()?;
    let is_ref = if input.peek(Token![ref]) {
        input.parse::<Token![ref]>()?;
        true
    } else {
        false
    };
    let name = input.parse::<Ident>()?;
    input.parse::<Token![:]>()?;
    let type_annotation = input.parse::<Type>()?;
    input.parse::<Token![=]>()?;
    let default_value = input.parse::<Expr>()?;
    input.parse::<Token![;]>()?;

    Ok(MeaningField {
        is_ref,
        name,
        type_annotation,
        default_value,
    })
}

fn parse_meaning_method(input: ParseStream, meaning_name: &str) -> Result<MeaningMethodOrConstructor> {
    let attributes = Attribute::parse_outer(input)?;
    let visibility = input.parse::<Visibility>()?;
    let is_override = if input.peek(Token![override]) {
        input.parse::<Token![override]>()?;
        true
    } else {
        false
    };
    input.parse::<Token![fn]>()?;
    let mut is_constructor = false;
    let id = input.parse::<Ident>()?;
    if !is_override && id.to_string() == meaning_name {
        // id.span().unwrap().error("Identifier must be equals \"constructor\"").emit();
        is_constructor = true;
    }
    let mut generics = input.parse::<Generics>()?;

    let parens_content;
    parenthesized!(parens_content in input);
    let inputs = parens_content.parse_terminated(FnArg::parse, Comma)?;

    generics.where_clause = if input.peek(Token![where]) { Some(input.parse::<WhereClause>()?) } else { None };

    let braced_content;
    let brace_token = braced!(braced_content in input);

    if !is_constructor {
        return Ok(MeaningMethodOrConstructor::Method(MeaningMethod {
            attributes: RefCell::new(attributes),
            visibility,
            is_override,
            name: id,
            generics,
            inputs,
            statements: brace_token,
        }));
    }

    braced_content.parse::<Token![super]>()?;

    let paren_content;
    let _ = parenthesized!(paren_content in braced_content);
    let super_arguments = paren_content.parse_terminated(Expr::parse, Comma)?;
    braced_content.parse::<Token![;]>()?;

    let mut statements = vec![];
    while !braced_content.is_empty() {
        statements.push(braced_content.parse::<Stmt>()?);
    }

    Ok(MeaningMethodOrConstructor::Constructor(MeaningConstructor {
        attributes,
        visibility,
        generics,
        inputs,
        super_arguments,
        statements,
    }))
}

fn parse_meaning_arena_type_name(input: ParseStream) -> Result<Path> {
    input.parse::<Token![type]>()?;
    let id = input.parse::<Ident>()?;
    if id.to_string() != "Arena" {
        id.span().unwrap().error("Identifier must be equals \"Arena\"").emit();
    }
    input.parse::<Token![=]>()?;
    let path = Path::parse_mod_style(input)?;
    input.parse::<Token![;]>()?;
    Ok(path)
}

#[proc_macro]
pub fn smodel(input: TokenStream) -> TokenStream {
    let MeaningTree {
        arena_type_name, meanings
    } = parse_macro_input!(input as MeaningTree);

    let mut host = LmtHost::new();

    // # Validations

    // 1. Ensure there is at least one meaning.

    if meanings.is_empty() {
        panic!("There must be at least one meaning.");
    }

    // 2. Ensure the first meaning inherits no other one.

    if meanings[0].inherits.is_some() {
        meanings[0].name.span().unwrap().error("First meaning must inherit no any other meaning.").emit();
        return TokenStream::new();
    }
    let base_meaning_name = meanings[0].name.to_string();

    // 3. Ensure all other meanings inherit another one.

    for m in meanings[1..].iter() {
        if m.inherits.is_none() {
            m.name.span().unwrap().error("Meaning must inherit another meaning.").emit();
            return TokenStream::new();
        }
    }

    // # Processing steps

    // 1. Output the arena type.
    host.output.extend::<TokenStream>(quote! {
        type #arena_type_name = ::smodel::Arena<__data__::#base_meaning_name>;
    }.try_into().unwrap());

    // 2. Traverse each meaning in a first pass.
    for meaning_node in meanings.iter() {
        ProcessingStep2().exec(&mut host, meaning_node);
    }

    // 3. Traverse each meaning.
    for meaning_node in meanings.iter() {
        let Some(meaning) = host.semantics.get(meaning_node) else {
            continue;
        };

        let asc_meaning_list = meaning.asc_meaning_list();
        let mut field_output = TokenStream::new();
        let meaning_name = meaning.name();

        // 3.1. Write out the base data accessor
        //
        // For example, for the base meaning data type, this
        // is always "self.0"; for a direct submeaning of the base
        // data type, this is always "self.0.0".

        let mut base_accessor = "self.0".to_owned();
        let mut m1 = meaning.clone();
        while let Some(m2) = m1.inherits() {
            base_accessor.push_str(".0");
            m1 = m2;
        }

        // 3.2. Traverse each field.
        for field in meaning_node.fields.iter() {
            ProcessingStep3_2().exec(&mut host, &meaning, field, &base_accessor, &asc_meaning_list, &mut field_output);
        }

        // 3.3. Contribute a #DATA_VARIANT_FIELD field to __data__::M
        // holding the enumeration of submeanings.
        let submeaning_enum = DATA_VARIANT_PREFIX.to_owned() + &meaning_name;
        field_output.extend::<TokenStream>(quote! {
            pub #DATA_VARIANT_FIELD: #submeaning_enum,
        }.try_into().unwrap());

        // 3.4. Contribute a #[non_exhaustive] enumeration of submeanings at the `__data__` module.
        let mut variants: Vec<String> = vec![];
        for submeaning in meaning.submeanings().iter() {
            let sn = submeaning.name();
            variants.push(format!("{sn}(::std::rc::Rc<__data__::{sn}>)"));
        }
        host.data_output.extend::<TokenStream>(quote! {
            #[non_exhaustive]
            pub enum #submeaning_enum {
                #(#variants),*
            }
        }.try_into().unwrap());

        // 3.5. Define the data structure __data__::M at the __data__ module output,
        // containing all field output.
        let field_output = field_output.to_string();
        host.data_output.extend::<TokenStream>(quote! {
            #[non_exhaustive]
            pub struct #meaning_name {
                #field_output
            }
        }.try_into().unwrap());

        // 3.6. Define the structure M
        ProcessingStep3_6().exec(&mut host, &meaning, &base_accessor);
    }

    // 4.

    todo!();

    // 5. Return output.

    host.output
}