view src/lexer/Token.d @ 206:d3c148ca429b

Major moving of files. all src now goes into src, all docs in docs.
author Anders Johnsen <skabet@gmail.com>
date Tue, 12 Aug 2008 18:14:56 +0200
parents
children e0551773a005
line wrap: on
line source

module lexer.Token;

public 
import basic.SourceLocation;

import Integer = tango.text.convert.Integer;

/** 
  The Token struct will be used through the Lexer, Parser and other
  modules as a location into source.

  The Token should always be optimized for size to limit unnecessary
  memory usage.
  */
struct Token
{
    Tok type;
    SLoc location;
    uint length;

    /**
      Create a new token with a Tok type, Location in source and a 
      length of how many chars the Token span in the source
      */
    static Token opCall (Tok type, SLoc location, uint length)
    {
        Token t;
        t.type = type;
        t.location = location;
        t.length = length;
        return t;
    }

    /**
      Get the type of the Token as a string
      */
    char[] getType ()
    {
        return typeToString[this.type];
    }

    /**
      A human readable dump of a Token
      */
    char[] toString ()
    {
        return this.getType()~": Len: "~Integer.toString(this.length);
    }

    /// Get the range of this token
    SourceRange asRange() { return SourceRange(location, location + length); }

    /**
      Returns true if the type of this token is a basic type (int, float, ...).
      Void is included, although a void in it self is not really a type.
     */
    bool isBasicType()
    {
        return type >= Tok.Byte && type <= Tok.Void;
    }

    /**
      Returns true for all the various assignments (=, +=, *= ...)
     */
    bool isAssignment()
    {
        return type == Tok.Assign;
    }

    /**
      Just a shortcut to avoid `token.type == Tok.Identifier`.
     */
    bool isIdentifier()
    {
        return type == Tok.Identifier;
    }
}

/**
  Tok is short for TokenType. This enum list is to supply the Token 
  with a type. 
  
  This enum is used to switch over "many" places.
  */
enum Tok : ushort
{
    /* Non-code related tokens */
    EOF,

    /* Basic types */
    Identifier,
    Integer,

    /* Basic operators */
    Assign,
    Plus, Minus,
    Star, Slash, Percent,
    Comma,

    /* Symbols */
    OpenParentheses,
    CloseParentheses,
    OpenBrace,
    CloseBrace,
    OpenBracket,
    CloseBracket,
    Seperator,
    Colon,
    Dot,

    /* Comparator operators */
    Eq, Ne,
    Lt, Gt,
    Le, Ge,

    Not,

    /* Keywords */
    Byte, Ubyte,
    Short, Ushort,
    Int, Uint,
    Long, Ulong,

    Char, Wchar, Dchar,

    Float, Double,

    Bool,

    Void,

    Struct,

    If, Else,
    While,
    Switch, Case, Default,
    Return, Cast,

    String,

    Module, Import,

}

/**
  An associative array to supply a Tok to String function.

  Keep always this list updated when adding a new Tok.
  */
public char[][Tok] typeToString;

static this()
{
    typeToString =
    [
        Tok.EOF:"EOF"[],
        Tok.Identifier:"Identifier",
        Tok.Byte:"Byte",
        Tok.Short:"Short",
        Tok.Int:"Int",
        Tok.Long:"Long",
        Tok.Char:"Char",
        Tok.Wchar:"Wchar",
        Tok.Dchar:"Dchar",
        Tok.Bool:"Bool",
        Tok.Void:"Void",
        Tok.Eq:"Eq",
        Tok.Ne:"Ne",
        Tok.Lt:"Lt",
        Tok.Le:"Le",
        Tok.Gt:"Gt",
        Tok.Ge:"Ge",
        Tok.OpenParentheses:"OpenParentheses",
        Tok.CloseParentheses:"CloseParentheses",
        Tok.OpenBrace:"OpenBrace",
        Tok.CloseBrace:"CloseBrace",
        Tok.OpenBracket:"OpenBracket",
        Tok.CloseBracket:"CloseBracket",
        Tok.Dot:"Dot",
        Tok.Assign:"Assign",
        Tok.Plus:"Plus",
        Tok.Minus:"Minus",
        Tok.Star:"Star",
        Tok.Slash:"Slash",
        Tok.Percent:"Percent",
        Tok.Integer:"Integer",
        Tok.If:"If",
        Tok.While:"While",
        Tok.Switch:"Switch",
        Tok.Case:"Case",
        Tok.Default:"Default",
        Tok.Comma:"Comma",
        Tok.Return:"Return",
        Tok.Struct:"Struct",
        Tok.Colon:"Colon",
        Tok.Seperator:"Seperator",
        Tok.Cast:"Cast",
        Tok.Module:"Module",
        Tok.Import:"Import",
        Tok.String:"String"
    ];
}