<commit_before>// Copyright 2014 <NAME> (IRCAM)

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub use compile_error::*;
pub use syntax::parse::token::InternedString;
pub use syntax::ast::*;

use syntax::codemap::DUMMY_SP;
use std::rc::Rc;

#[deriving(Clone)]
pub struct AttributeValue<T>
{
  pub value: Option<T>,
  pub span: Span,
  duplicate: DuplicateAttribute
}

impl<T: Clone> AttributeValue<T>
{
  pub fn new(duplicate: DuplicateAttribute) -> AttributeValue<T>
  {
    AttributeValue {
      value: None,
      duplicate: duplicate,
      span: DUMMY_SP
    }
  }

  pub fn simple() -> AttributeValue<T>
  {
    AttributeValue::new(DuplicateAttribute::simple(Warn))
  }

  pub fn update(self, cx: &ExtCtxt, value: T, span: Span) -> AttributeValue<T>
  {
    let mut this = self;
    if this.value.is_some() {
      this.duplicate.issue(cx, this.span, span);
    } else {
      this.value = Some(value);
      this.span = span;
    }
    this
  }

  pub fn has_value(&self) -> bool
  {
    self.value.is_some()
  }

  pub fn value_or(&self, default: T) -> T
  {
    match self.value {
      None => default,
      Some(ref value) => value.clone()
    }
  }

  pub fn span(&self) -> Span
  {
    self.span.clone()
  }
}

#[deriving(Clone)]
pub struct AttributeInfo
{
  pub name: InternedString,
  pub desc: &'static str,
  pub model: AttributeModel
}

impl AttributeInfo
{
  pub fn new(name: &'static str, desc: &'static str, 
    model: AttributeModel) -> AttributeInfo
  {
    AttributeInfo {
      name: InternedString::new(name),
      desc: desc,
      model: model
    }
  }

  pub fn simple(name: &'static str, desc: &'static str) -> AttributeInfo
  {
    AttributeInfo::new(name, desc, UnitValue(AttributeValue::simple()))
  }

  pub fn update(self, model: AttributeModel) -> AttributeInfo
  {
    let mut this = self;
    this.model = model;
    this
  }

  pub fn plain_value<'a>(&'a self) -> &'a AttributeValue<()>
  {
    match self.model {
      UnitValue(ref v) => v,
      _ => fail!("No plain value for the current attribute.")
    }
  }

  pub fn sub_model<'a>(&'a self) -> &'a AttributeArray
  {
    match self.model {
      SubAttribute(ref array) => array,
      _ => fail!("No sub value for the attribute.")
    }
  }
}

#[deriving(Clone)]
pub enum AttributeModel
{
  UnitValue(AttributeValue<()>),
  KeyValue(AttributeLitModel),
  SubAttribute(AttributeArray)
}

#[deriving(Clone)]
pub enum AttributeLitModel
{
  MLitStr(AttributeValue<(InternedString, StrStyle)>),
  MLitBinary(AttributeValue<Rc<Vec<u8>>>),
  MLitByte(AttributeValue<u8>),
  MLitChar(AttributeValue<char>),
  MLitInt(AttributeValue<(u64, LitIntType)>),
  MLitFloat(AttributeValue<(InternedString, FloatTy)>),
  MLitFloatUnsuffixed(AttributeValue<InternedString>),
  MLitNil(AttributeValue<()>),
  MLitBool(AttributeValue<bool>)
}

pub type AttributeArray = Vec<AttributeInfo>;

pub mod access
{
  pub use super::*;
  pub fn by_name<'a>(array: &'a AttributeArray, name: &'static str) -> &'a AttributeInfo
  {
    let interned = InternedString::new(name);
    for info in array.iter() {
      if info.name == interned {
        return info;
      }
    }
    fail!("Try to get an attribute that doesn't exist.")
  }

  pub fn plain_value<'a>(array: &'a AttributeArray, name: &'static str) -> &'a AttributeValue<()>
  {
    by_name(array, name).plain_value()
  }

  pub fn sub_model<'a>(array: &'a AttributeArray, name: &'static str) -> &'a AttributeArray
  {
    by_name(array, name).sub_model()
  }

  pub fn plain_value_or(array: &AttributeArray, name: &'static str, def: bool) -> bool
  {<commit_msg>[Access] Add access to the string literal.<commit_after>// Copyright 2014 <NAME> (IRCAM)

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub use compile_error::*;
pub use syntax::parse::token::InternedString;
pub use syntax::ast::*;

use syntax::codemap::DUMMY_SP;
use std::rc::Rc;

#[deriving(Clone)]
pub struct AttributeValue<T>
{
  pub value: Option<T>,
  pub span: Span,
  duplicate: DuplicateAttribute
}

impl<T: Clone> AttributeValue<T>
{
  pub fn new(duplicate: DuplicateAttribute) -> AttributeValue<T>
  {
    AttributeValue {
      value: None,
      duplicate: duplicate,
      span: DUMMY_SP
    }
  }

  pub fn simple() -> AttributeValue<T>
  {
    AttributeValue::new(DuplicateAttribute::simple(Warn))
  }

  pub fn update(self, cx: &ExtCtxt, value: T, span: Span) -> AttributeValue<T>
  {
    let mut this = self;
    if this.value.is_some() {
      this.duplicate.issue(cx, this.span, span);
    } else {
      this.value = Some(value);
      this.span = span;
    }
    this
  }

  pub fn has_value(&self) -> bool
  {
    self.value.is_some()
  }

  pub fn value_or(&self, default: T) -> T
  {
    match self.value {
      None => default,
      Some(ref value) => value.clone()
    }
  }

  pub fn span(&self) -> Span
  {
    self.span.clone()
  }
}

#[deriving(Clone)]
pub struct AttributeInfo
{
  pub name: InternedString,
  pub desc: &'static str,
  pub model: AttributeModel
}

impl AttributeInfo
{
  pub fn new(name: &'static str, desc: &'static str, 
    model: AttributeModel) -> AttributeInfo
  {
    AttributeInfo {
      name: InternedString::new(name),
      desc: desc,
      model: model
    }
  }

  pub fn simple(name: &'static str, desc: &'static str) -> AttributeInfo
  {
    AttributeInfo::new(name, desc, UnitValue(AttributeValue::simple()))
  }

  pub fn update(self, model: AttributeModel) -> AttributeInfo
  {
    let mut this = self;
    this.model = model;
    this
  }

  pub fn plain_value<'a>(&'a self) -> &'a AttributeValue<()>
  {
    match self.model {
      UnitValue(ref v) => v,
      _ => fail!("No plain value for the current attribute.")
    }
  }

  pub fn sub_model<'a>(&'a self) -> &'a AttributeArray
  {
    match self.model {
      SubAttribute(ref array) => array,
      _ => fail!("No sub value for the current attribute.")
    }
  }

  pub fn key_value<'a>(&'a self) -> &'a AttributeLitModel
  {
    match self.model {
      KeyValue(ref lit) => lit,
      _ => fail!("No key value for the current attribute.")
    }
  }
}

#[deriving(Clone)]
pub enum AttributeModel
{
  UnitValue(AttributeValue<()>),
  KeyValue(AttributeLitModel),
  SubAttribute(AttributeArray)
}

#[deriving(Clone)]
pub enum AttributeLitModel
{
  MLitStr(AttributeValue<(InternedString, StrStyle)>),
  MLitBinary(AttributeValue<Rc<Vec<u8>>>),
  MLitByte(AttributeValue<u8>),
  MLitChar(AttributeValue<char>),
  MLitInt(AttributeValue<(u64, LitIntType)>),
  MLitFloat(AttributeValue<(InternedString, FloatTy)>),
  MLitFloatUnsuffixed(AttributeValue<InternedString>),
  MLitNil(AttributeValue<()>),
  MLitBool(AttributeValue<bool>)
}

pub type AttributeArray = Vec<AttributeInfo>;

pub mod access
{
  pub use super::*;
  pub fn by_name<'a>(array: &'a AttributeArray, name: &'static str) -> &'a AttributeInfo
  {
    let interned = InternedString::new(name);
    for info in array.iter() {
      if info.name == interned {
        return info;
      }
    }
    fail!("Try to get an attribute that doesn't exist.")
  }

  pub fn plain_value<'a>(array: &'a AttributeArray, name: &'static str) -> &'a AttributeValue<()>
  {
    by_name(array, name).plain_value()
  }

  pub fn sub_model<'a>(array: &'a AttributeArray, name: &'static str) -> &'a AttributeArray
  {
    by_name(array, name).sub_model()
  }

  pub fn lit_str<'a>(array: &'a AttributeArray, name: &'static str) -> Option<&'a AttributeValue<(InternedString, StrStyle)>>
  {
    match by_name(array, name).key_value() {
      &MLitStr(ref val) => Some(val),
      _ => None
    }
  }

  pub fn plain_value_or(array: &AttributeArray, name: &'static str, def: bool) -> bool
  {
{'fraction_non_alphanumeric': 0.13807820971276963, 'fraction_numerical': 0.0027684854077748297, 'mean_word_length': 3.432515337423313, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '208765', 'n_tokens_mistral': 2898, 'n_tokens_neox': 2894, 'n_words': 777}