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
//! Lexical analysis.

use std::str;
use std::fmt;

use kailua_diag::{Locale, Localize, Localized};
use string::{Name, Str};

/// A token.
#[derive(Clone, Debug, PartialEq)]
pub enum Tok {
    /// A token which is distinct from all other tokens.
    ///
    /// The lexer emits this token on an error.
    Error,

    /// A comment token. The parser should ignore this.
    Comment,

    /// A punctuation.
    Punct(Punct),

    /// A keyword.
    Keyword(Keyword),

    /// A number.
    Num(f64),

    /// A name (either an identifier or a quoted name in the meta block).
    Name(Name),

    /// A string (either `"string"` or `[[string]]`).
    Str(Str),

    /// The end of file.
    ///
    /// A valid stream of tokens is expected to have only one EOF token at the end.
    EOF,
}

impl Localize for Tok {
    fn fmt_localized(&self, f: &mut fmt::Formatter, locale: Locale) -> fmt::Result {
        match (&locale[..], self) {
            ("ko", &Tok::Error)      => write!(f, "잘못된 문자"),
            (_,    &Tok::Error)      => write!(f, "an invalid character"),
            ("ko", &Tok::Comment)    => write!(f, "주석"),
            (_,    &Tok::Comment)    => write!(f, "a comment"),
            (_,    &Tok::Punct(p))   => write!(f, "{}", Localized::new(&p, locale)),
            (_,    &Tok::Keyword(w)) => write!(f, "{}", Localized::new(&w, locale)),
            ("ko", &Tok::Num(_))     => write!(f, "숫자"),
            (_,    &Tok::Num(_))     => write!(f, "a number"),
            ("ko", &Tok::Name(_))    => write!(f, "이름"),
            (_,    &Tok::Name(_))    => write!(f, "a name"),
            ("ko", &Tok::Str(_))     => write!(f, "문자열 리터럴"),
            (_,    &Tok::Str(_))     => write!(f, "a string literal"),
            ("ko", &Tok::EOF)        => write!(f, "파일의 끝"),
            (_,    &Tok::EOF)        => write!(f, "the end of file"),
        }
    }
}

impl<'a> Localize for &'a Tok {
    fn fmt_localized(&self, f: &mut fmt::Formatter, locale: Locale) -> fmt::Result {
        (**self).fmt_localized(f, locale)
    }
}

macro_rules! define_puncts {
    ($ty:ident |$locale:ident|: $($i:ident $t:expr, #[$m:meta])*) => (
        /// A punctuation.
        ///
        /// This includes Kailua-specific punctuations,
        /// which are only generated in the meta block (marked as [M] below).
        /// Some of them are also only generated after a particular Lua version
        /// (marked as [5.x+] below).
        #[derive(Copy, Clone, Debug, PartialEq, Eq)]
        pub enum $ty { $(#[$m] $i,)* }

        impl Localize for $ty {
            fn fmt_localized(&self, f: &mut fmt::Formatter, $locale: Locale) -> fmt::Result {
                let text = match *self { $($ty::$i => $t,)* };
                fmt::Display::fmt(text, f)
            }
        }
    );
}

define_puncts! { Punct |locale|:
    Plus        "`+`",      /// `+`.
    Dash        "`-`",      /// `-`.
    Star        "`*`",      /// `*`.
    Slash       "`/`",      /// `/`.
    Percent     "`%`",      /// `%`.
    Caret       "`^`",      /// `^`.
    Hash        "`#`",      /// `#`.
    EqEq        "`==`",     /// `==`.
    TildeEq     "`~=`",     /// `~=`.
    LtEq        "`<=`",     /// `<=`.
    GtEq        "`>=`",     /// `>=`.
    Lt          "`<`",      /// `<`.
    Gt          "`>`",      /// `>`.
    Eq          "`=`",      /// `=`.
    Amp         "`&`",      /// `&`. [5.3+]
    Tilde       "`~`",      /// `~`. [5.3+]
    Pipe        "`|`",      /// `|`. [5.3+ or M]
    LtLt        "`<<`",     /// `<<`. [5.3+]
    GtGt        "`>>`",     /// `>>`. [5.3+]
    SlashSlash  "`//`",     /// `//`. [5.3+]
    LParen      "`(`",      /// `(`.
    RParen      "`)`",      /// `)`.
    LBrace      "`{`",      /// `{`.
    RBrace      "`}`",      /// `}`.
    LBracket    "`[`",      /// `[`.
    RBracket    "`]`",      /// `]`.
    Semicolon   "`;`",      /// `;`.
    Colon       "`:`",      /// `:`.
    ColonColon  "`::`",     /// `::`. [5.2+]
    Comma       "`,`",      /// `,`.
    Dot         "`.`",      /// `.`.
    DotDot      "`..`",     /// `..`.
    DotDotDot   "`...`",    /// `...`.

    // Kailua extensions
    DashDashHash    "`--#`",    /// `--#`. [M]
    DashDashV       "`--v`",    /// `--v`. [M]
    DashDashColon   "`--:`",    /// `--:`. [M]
    DashDashGt      "`-->`",    /// `-->`. [M]
    Ques            "`?`",      /// `?`. [M]
    Bang            "`!`",      /// `!`. [M]
    Newline         match &locale[..] { "ko" => "개행문자", _ => "a newline" },
                    /// A newline. Only generated at the end of the meta block.
}

macro_rules! define_keywords {
    ($ty:ident: everywhere { $($i:ident $t:expr, #[$m:meta])* }
                meta_only { $($mi:ident $mt:expr, #[$mm:meta])* }) => (
        /// A keyword.
        ///
        /// This includes Kailua-specific keywords,
        /// which are only generated in the meta block (marked as [M] below).
        /// Some of them are also only generated after a particular Lua version
        /// (marked as [5.x+] below).
        #[derive(Copy, Clone, Debug, PartialEq, Eq)]
        pub enum $ty { $(#[$m] $i,)* $(#[$mm] $mi,)* }

        impl $ty {
            pub fn from(s: &[u8], in_meta: bool) -> Option<Keyword> {
                match (in_meta, s) {
                    $((_, $t) => Some(Keyword::$i),)*
                    $((true, $mt) => Some(Keyword::$mi),)*
                    (_, _) => None,
                }
            }

            pub fn name(&self) -> &'static [u8] {
                match *self { $($ty::$i => $t,)* $($ty::$mi => $mt,)* }
            }
        }
    );
}

define_keywords! { Keyword:
    everywhere {
        And         b"and",         /// `and`.
        Break       b"break",       /// `break`.
        Do          b"do",          /// `do`.
        Else        b"else",        /// `else`.
        Elseif      b"elseif",      /// `elseif`.
        End         b"end",         /// `end`.
        False       b"false",       /// `false`.
        For         b"for",         /// `for`.
        Function    b"function",    /// `function`.
        Goto        b"goto",        /// `goto`. [5.2+; a normal identifier in Lua 5.1]
        If          b"if",          /// `if`.
        In          b"in",          /// `in`.
        Local       b"local",       /// `local`.
        Nil         b"nil",         /// `nil`.
        Not         b"not",         /// `not`.
        Or          b"or",          /// `or`.
        Repeat      b"repeat",      /// `repeat`.
        Return      b"return",      /// `return`.
        Then        b"then",        /// `then`.
        True        b"true",        /// `true`.
        Until       b"until",       /// `until`.
        While       b"while",       /// `while`.
    }

    meta_only { // Kailua extensions
        Assume      b"assume",      /// `assume`. [M]
        Class       b"class",       /// `class`. [M]
        Const       b"const",       /// `const`. [M]
        Global      b"global",      /// `global`. [M]
        Map         b"map",         /// `map`. [M]
        Method      b"method",      /// `method`. [M]
        Module      b"module",      /// `module`. [M]
        Once        b"once",        /// `once`. [M]
        Open        b"open",        /// `open`. [M]
        Static      b"static",      /// `static`. [M]
        Type        b"type",        /// `type`. [M]
        Var         b"var",         /// `var`. [M]
        Vector      b"vector",      /// `vector`. [M]
    }
}

impl From<Keyword> for Str {
    fn from(kw: Keyword) -> Str {
        kw.name().into()
    }
}

impl From<Keyword> for Name {
    fn from(kw: Keyword) -> Name {
        kw.name().into()
    }
}

impl Localize for Keyword {
    fn fmt_localized(&self, f: &mut fmt::Formatter, locale: Locale) -> fmt::Result {
        let name = str::from_utf8(self.name()).unwrap();
        match &locale[..] {
            "ko" => write!(f, "예약어 `{}`", name),
            _ => write!(f, "a keyword `{}`", name),
        }
    }
}

mod lexer;
mod nesting;

pub use self::lexer::Lexer;
pub use self::nesting::{Nest, NestedToken, NestingCategory, NestingSerial};