#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#    This file is part of arena.
#   
#    arena is free software: you can redistribute it and/or modify it
#    under the terms of the GNU General Public License as published by the
#    Free Software Foundation; either version 3 of the License or, at your
#    option, any later version.
#
#    arena is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABLITY
#    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
#    License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with arena.  If not, see <http://www.gnu.org/licenses/>. 

## @file
#  This file defines a class that helps to determine the semantics of the
#  C computer language, for syntax highlighting.

from Library.Parse.Lex.Languages import AComputerLanguage, CharacterType, \
    TokenType
    
## C opcodes understood by the parser
COpcodes = {
    "case": TokenType.keyword,
    "char": TokenType.typeIdentifier,
    "const": TokenType.keyword,
    "continue": TokenType.keyword,
    "default": TokenType.keyword,
    "do": TokenType.keyword,
    "double": TokenType.typeIdentifier,
    "else": TokenType.keyword,
    "enum": TokenType.keyword,
    "extern": TokenType.keyword,
    "float": TokenType.typeIdentifier,
    "for": TokenType.keyword,
    "goto": TokenType.keyword,
    "if": TokenType.keyword,
    "inline": TokenType.keyword,
    "int": TokenType.typeIdentifier,
    "long": TokenType.typeIdentifier,
    "register": TokenType.keyword,
    "return": TokenType.keyword,
    "short": TokenType.keyword,
    "signed": TokenType.keyword,
    "sizeof": TokenType.keyword,
    "static": TokenType.keyword,
    "struct": TokenType.typeIdentifier,
    "switch": TokenType.keyword,
    "typedef": TokenType.keyword,
    "union": TokenType.typeIdentifier,
    "unsigned": TokenType.keyword,
    "void": TokenType.typeIdentifier,
    "volatile": TokenType.keyword,
    "while": TokenType.keyword,
    "+": TokenType.operator,
    "-": TokenType.operator,
    "*": TokenType.operator,
    "/": TokenType.operator,
    "\\": TokenType.operator,
    "=": TokenType.special,
    "++": TokenType.operator,
    "--": TokenType.operator,
    "+=": TokenType.operator,
    "-=": TokenType.operator,
    "*=": TokenType.operator,
    "/=": TokenType.operator,
    "\\=": TokenType.operator,
    "^": TokenType.operator,
    "^=": TokenType.operator,
    "&amp;": TokenType.operator,
    "&amp;&amp;": TokenType.operator,
    "&amp;=": TokenType.operator,
    "|": TokenType.operator,
    "||": TokenType.operator,
    "!": TokenType.operator,
    "&lt;": TokenType.operator,
    "&lt;=": TokenType.operator,
    "&lt;&lt;": TokenType.operator,
    ">": TokenType.operator,
    ">=": TokenType.operator,
    ">>": TokenType.operator,
    "->": TokenType.operator,
    "?": TokenType.operator,
    "\"": TokenType.string,
    "\'": TokenType.string,
    "\\n": TokenType.special,
    "\\r": TokenType.special,
    "\\t": TokenType.special,
    "\\x": TokenType.special,
    "/*": TokenType.special,
    "*/": TokenType.special,
    "//": TokenType.special,
    "{": TokenType.special,
    "}": TokenType.special   
}

## C syntax rulesets
CRules = {
    "comment/line": ("#", "//"),
    "comment/block/begin": ("/*",),
    "comment/block/end": ("*/",),
    "string/escape": ("\\n", "\\r", "\\t", "\\x")
}

## C character types
CCharacterTypes = {
    CharacterType.symbol: "()[]{}@,:.`=+-*/>%|^\"\'!",
    CharacterType.letter: ("\\$&_ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                           "abcdefghijklmnopqrstuvwxyz"),
    CharacterType.alpha: ";",
    CharacterType.digit: "0123456789",
    CharacterType.numeric: ".-ABCDEFXabcdefx",
    CharacterType.space: ("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C"
                          "\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19"
                          "\x1A\x1B\x1C\x1D\x1E\x1F\x20\x7F"),
    CharacterType.quote: "",
    CharacterType.endOfLine: "\x0A\x0D",
    CharacterType.endOfStream: "\x19"
}

## A class that represents the syntax and rules of the C language
class c(AComputerLanguage):
    ## Constructor
    def init(Self):
        result = AComputerLanguage.init(Self)
        
        if result:
            # Set private attributes
            # This language is case-sensitive
            Self._caseAware = True
            # Opcodes recognized by the language
            Self.registerOpcodes(**COpcodes)
            # Rules of the language
            Self._Rules.update(**CRules)
            # Character types recognized by the language
            for key in CCharacterTypes.iterkeys():
                Self.characters[key] = CCharacterTypes[key]
                
        return result
