Skip to content

Commit

Permalink
Playing with sized arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
rsheeter committed Oct 20, 2022
1 parent ffab7b7 commit 523bd1a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1,804 deletions.
12 changes: 9 additions & 3 deletions font-codegen/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,9 +1053,15 @@ impl FieldType {
pub(crate) fn cooked_type_tokens(&self) -> &syn::Ident {
match &self {
FieldType::Offset { typ, .. } | FieldType::Scalar { typ } => typ,
FieldType::Other { typ } => typ
.get_ident()
.expect("non-trivial custom types never cooked"),
FieldType::Other { typ } => {
match typ.get_ident() {
Some(ident) => ident,
None => {
eprintln!("What do I do with {:#?}", self);
panic!("Oh no");
}
}
},
FieldType::Array { .. }
| FieldType::ComputedArray { .. }
| FieldType::VarLenArray(_) => {
Expand Down
26 changes: 24 additions & 2 deletions font-codegen/src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
use std::collections::HashMap;

use proc_macro2::{Span, TokenStream};
use proc_macro2::{Span, TokenStream, Ident, Punct};
use quote::{quote, ToTokens};
use syn::{
braced, bracketed, parenthesized,
parse::{Parse, ParseStream},
punctuated::Punctuated,
spanned::Spanned,
token, Attribute, Token,
token, Attribute, Token
};

pub(crate) struct Items {
Expand Down Expand Up @@ -283,6 +283,10 @@ pub(crate) enum FieldType {
},
ComputedArray(CustomArray),
VarLenArray(CustomArray),
SizedArray {
typ: syn::Ident,
len: syn::Ident,
}
}

/// A representation shared between computed & varlen arrays
Expand Down Expand Up @@ -587,6 +591,24 @@ impl Parse for Field {

impl Parse for FieldType {
fn parse(input: ParseStream) -> syn::Result<Self> {
if input.peek(token::Bracket) {
// An array. It should be Ident, Punct(;), Ident
let content;
bracketed!(content in input);

let array_type: Ident = content.parse()?;
let punct = content.parse::<Punct>()?;
if punct.as_char() != ';' {
return Err(syn::Error::new(punct.span(), "Invalid separator"));
}
let array_len: Ident = content.parse()?;

// Should be no further content
if !content.is_empty() {
return Err(syn::Error::new(content.span(), "Unexpected content"));
}
return Ok(FieldType::SizedArray { typ: array_type, len: array_len });
}
if input.lookahead1().peek(token::Bracket) {
let content;
bracketed!(content in input);
Expand Down
Loading

0 comments on commit 523bd1a

Please sign in to comment.