Changeset 2124


Ignore:
Timestamp:
Dec 1, 2012, 1:08:08 PM (11 years ago)
Author:
sam
Message:

vslol: refactor regexes to use word lists instead.

Location:
trunk/tools/vslol
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/vslol/CppKeywordClassifier.cs

    r2123 r2124  
    3131{
    3232    [Import]
    33     internal IClassificationTypeRegistryService m_type_registry = null; /* Set via MEF */
     33    internal IClassificationTypeRegistryService m_type_registry = null;
    3434    [Import]
    3535    internal IClassifierAggregatorService m_aggregator = null;
     
    6060    private IClassifier m_classifier;
    6161
    62     private IClassificationType m_customclass_type, m_constant_type;
    63     private Regex m_customclass_regex, m_constant_regex;
     62    private IClassificationType m_types_type, m_constant_type;
     63    private Regex m_types_regex, m_constant_regex;
     64
     65    private static const string[] m_all_types =
     66    {
     67        "auto|void|bool|int|unsigned|char|short|long|float|double";
     68        "class|struct|union|template|namespace|typename|typedef",
     69        "const|static|extern|volatile|inline|explicit"
     70    };
     71
     72    private static const string[] m_cpp_types =
     73    {
     74        "u?int(8|16|32|64|ptr)_t",
     75        "(wchar|size|ssize)_t",
     76        "va_list",
     77    };
     78
     79    /* ldouble real half
     80       "(f(16|128)||d|[ui](8|16||64)|r)(vec[234]|mat[234]|quat|cmplx)";
     81     */
     82
     83    private static const string[] m_csharp_types =
     84    {
     85        "var|string",
     86        "out|ref|internal|sealed|public|private|protected|override"
     87    };
     88
     89    private static const string[] m_lolfx_types =
     90    {
     91        "attribute|varying|uniform|in|out",
     92        "int|uint",
     93        "(|[dui])(vec|mat)[234]"
     94    };
     95
     96    private static const string[] m_all_constants =
     97    {
     98        "true|false"
     99    };
     100
     101    private static const string[] m_cpp_constants =
     102    {
     103        "NULL|nullptr",
     104        "EXIT_SUCCESS|EXIT_FAILURE",
     105        "M_(E|LOG(2|10)E|LN2|LN10|PI|PI_2|PI_4|1_PI|2_PI|2_SQRTPI|SQRT(2|1_2))",
     106        "SIG(HUP|INT|QUIT|ILL|TRAP|ABRT|FPE|KILL|USR1|SEGV|USR2|PIPE|ALRM)",
     107        "SIG(TERM|CHLD|CONT|STOP|TSTP|TTIN|TTOU)"
     108    };
     109
     110    private static const string[] m_csharp_constants =
     111    {
     112        "null",
     113    };
     114
     115    private static const string[] m_lolfx_constants =
     116    {
     117        "gl_Position|gl_FragColor",
     118    };
    64119
    65120    internal CppKeywordClassifier(IClassificationTypeRegistryService registry,
     
    70125
    71126        /* Regex for types and specifiers */
    72         m_customclass_type = registry.GetClassificationType("LolCustomClass");
    73 
    74         string tmp = @"\b(";
    75         tmp += "void|bool|int|unsigned|char|short|long|float|double|ldouble|";
    76         tmp += "class|struct|union|template|const|static|extern|volatile|inline|namespace|";
     127        m_types_type = registry.GetClassificationType("LolAnyType");
     128
     129        List<string> types_list = m_all_types.ToList();
     130        if (type.IsOfType("c/c++"))
     131            types_list = types_list.Concat(m_cpp_types).ToList();
     132        if (type.IsOfType("csharp"))
     133            types_list = types_list.Concat(m_csharp_types).ToList();
    77134        if (type.IsOfType("lolfx"))
    78             tmp += "attribute|varying|uniform|in|out|";
     135            types_list = types_list.Concat(m_lolfx_types).ToList();
     136        m_types_regex =
     137            new Regex(@"\b(" + String.Join("|", types_list.ToArray()) + @")\b");
     138
     139        /* Regex for constant words */
     140        m_constant_type = registry.GetClassificationType("LolAnyConstant");
     141
     142        List<string> constants_list = m_all_constants.ToList();
     143        if (type.IsOfType("c/c++"))
     144            constants_list = constants_list.Concat(m_cpp_constants).ToList();
    79145        if (type.IsOfType("csharp"))
    80             tmp += "var|out|ref|string|internal|sealed|public|private|protected|override|";
    81         if (!type.IsOfType("csharp"))
    82             tmp += "(f(16|128)||d|[ui](8|16||64)|r)(vec[234]|mat[234]|quat|cmplx)|";
    83         if (type.IsOfType("c/c++"))
    84         {
    85             tmp += "u?int(8|16|32|64|ptr)_t|";
    86             tmp += "(wchar|size|ssize)_t|";
    87             tmp += "real|half|explicit|typename|typedef|auto|";
    88         }
    89         tmp = tmp.Remove(tmp.Length - 1);
    90         tmp += @")\b";
    91         m_customclass_regex = new Regex(tmp);
    92 
    93         /* Regex for constant words */
    94         m_constant_type = registry.GetClassificationType("LolCppConstant");
    95 
    96         if (type.IsOfType("csharp"))
    97             m_constant_regex = new Regex(@"\b(null|true|false)\b");
    98         else if (type.IsOfType("c/c++"))
    99             m_constant_regex = new Regex(@"\b(NULL|nullptr|true|false|M_PI)\b");
    100         else if (type.IsOfType("lolfx"))
    101             m_constant_regex = new Regex(@"\b(gl_Position|gl_FragColor)\b");
    102         else
    103             m_constant_regex = new Regex(@"\b(NULL)\b");
    104 
     146            constants_list = constants_list.Concat(m_csharp_constants).ToList();
     147        if (type.IsOfType("lolfx"))
     148            constants_list = constants_list.Concat(m_lolfx_constants).ToList();
     149        m_constant_regex =
     150            new Regex(@"\b(" + String.Join("|", constants_list.ToArray()) + @")\b");
    105151    }
    106152
     
    116162            if (cs_class == "keyword" || cs_class == "identifier")
    117163            {
    118                 if (m_customclass_regex.IsMatch(cs.Span.GetText()))
     164                if (m_types_regex.IsMatch(cs.Span.GetText()))
    119165                {
    120                     ret.Add(new ClassificationSpan(cs.Span, m_customclass_type));
     166                    ret.Add(new ClassificationSpan(cs.Span, m_types_type));
    121167                    continue;
    122168                }
     
    202248internal sealed class LolCppTypeFormat : LolGenericFormat
    203249{
    204     public const string m_name = "LolCustomClass";
     250    public const string m_name = "LolAnyType";
    205251    public LolCppTypeFormat()
    206252    {
     
    220266internal sealed class LolCppConstantFormat : LolGenericFormat
    221267{
    222     public const string m_name = "LolCppConstant";
     268    public const string m_name = "LolAnyConstant";
    223269    public LolCppConstantFormat()
    224270    {
  • trunk/tools/vslol/LolFxLanguageService.cs

    r2123 r2124  
    99//
    1010
     11using System;
    1112using System.Collections.Generic;
    1213using System.Linq;
Note: See TracChangeset for help on using the changeset viewer.