modernc.org/cc@v1.0.1/scanner.l (about)

     1  %{
     2  // Copyright 2016 The CC Authors. All rights reserved.
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  // Based on [0], 6.4.
     7  %}
     8  
     9  %yyc c
    10  %yyn c = l.Next()
    11  %yym l.Mark()
    12  %yyt l.sc
    13  
    14  %x COMMENT
    15  %s DEFINE DIRECTIVE HEADER
    16  
    17  %{
    18  package cc
    19  
    20  import (
    21          "fmt"
    22  
    23          "modernc.org/golex/lex"
    24  )
    25  
    26  const (
    27          _ = iota
    28          scCOMMENT       // [`/*`, `*/`]
    29          scDEFINE        // [^#define, next token]
    30          scDIRECTIVE     // [^#, next token]
    31          scHEADER        // [`#include`, next token]
    32  )
    33  
    34  func (l *lexer) scan() (r int) {
    35          c := l.Enter()
    36  %}
    37  
    38  binary-exponent-part            [pP]{sign}?{digit-sequence}
    39  c-char                          [^'\n\x80\\]|{escape-sequence}
    40  c-char-sequence                 {c-char}+
    41  character-constant              '{c-char-sequence}'
    42  comment-close                   ([^*\x80]|\*+[^*/\x80])*\*+\/
    43  decimal-constant                {nonzero-digit}{digit}*
    44  decimal-floating-constant       ({fractional-constant}{exponent-part}?|{digit-sequence}{exponent-part}){floating-suffix}?
    45  digit                           [0-9]
    46  digit-sequence                  {digit}+
    47  eof                             \x80
    48  escape-sequence                 {simple-sequence}|{octal-escape-sequence}|{hexadecimal-escape-sequence}|{universal-character-name}
    49  exponent-part                   [eE]{sign}?{digit-sequence}
    50  floating-constant               {decimal-floating-constant}|{hexadecimal-floating-constant}
    51  floating-suffix                 i?[flFL]?|[flFL]?i?
    52  fractional-constant             {digit-sequence}?\.{digit-sequence}|{digit-sequence}\.
    53  h-char                          [^>\n\x80]
    54  h-char-sequence                 {h-char}+
    55  header-name                     <{h-char-sequence}>|\x22{q-char-sequence}\x22
    56  hex-quad                        {hexadecimal-digit}{hexadecimal-digit}{hexadecimal-digit}{hexadecimal-digit}
    57  hexadecimal-constant            {hexadecimal-prefix}{hexadecimal-digit}+
    58  hexadecimal-digit               [0-9a-fA-F]
    59  hexadecimal-digit-sequence      {hexadecimal-digit}+
    60  hexadecimal-escape-sequence     \\x{hexadecimal-digit}+
    61  hexadecimal-floating-constant   {hexadecimal-prefix}({hexadecimal-fractional-constant}|{hexadecimal-digit-sequence}){binary-exponent-part}{floating-suffix}?
    62  hexadecimal-fractional-constant {hexadecimal-digit-sequence}?\.{hexadecimal-digit-sequence}|{hexadecimal-digit-sequence}\.
    63  hexadecimal-prefix              0[xX]
    64  identifier                      {identifier-nondigit}({identifier-nondigit}|{digit}|{ucn-digit}|"$")*
    65  identifier-nondigit             {nondigit}|{universal-character-name}|{ucn-nondigit}
    66  integer-constant                ({decimal-constant}|{octal-constant}|{hexadecimal-constant}){integer-suffix}?
    67  integer-suffix                  {unsigned-suffix}({long-suffix}?|{long-long-suffix})|{long-suffix}{unsigned-suffix}?|{long-long-suffix}{unsigned-suffix}?
    68  long-long-suffix                ll|LL
    69  long-suffix                     [lL]
    70  nondigit                        [_a-zA-Z]
    71  nonzero-digit                   [1-9]
    72  octal-constant                  0{octal-digit}*
    73  octal-digit                     [0-7]
    74  octal-escape-sequence           \\{octal-digit}{octal-digit}?{octal-digit}?
    75  pp-number                       ({digit}|\.{digit})({digit}|{identifier-nondigit}|[eEpP]{sign}|\.)*
    76  q-char                          [^\n\x22\x80]
    77  q-char-sequence                 {q-char}+
    78  s-char                          [^\x22\n\x80\\]|{escape-sequence}
    79  s-char-sequence                 {s-char}+
    80  sign                            [-+]
    81  simple-sequence                 \\['\x22?\\abfnrtv]
    82  string-literal                  \x22{s-char-sequence}?\x22
    83  ucn-digit                       \x83
    84  ucn-nondigit                    \x84
    85  universal-character-name        \\u{hex-quad}|\\U{hex-quad}{hex-quad}
    86  unsigned-suffix                 [uU]
    87  
    88  %%
    89                                  c = l.Rule0()
    90  
    91  [ \t\f\v]+			return ' '
    92  
    93  "//"[^\x80\n]*			l.comment(false)
    94  				return ' '
    95  
    96  "/*"				l.commentPos0 = l.First.Pos()
    97                                  l.push(scCOMMENT)
    98  
    99  <COMMENT>{comment-close}        l.pop()
   100  				l.First = lex.NewChar(l.commentPos0, l.First.Rune)
   101  				l.comment(true)
   102  				return ' '
   103  
   104  <COMMENT>{eof}                  l.report.Err(l.commentPos0, commentNotClosed)
   105                                  l.pop()
   106                                  return rune2class(lex.RuneEOF)
   107  
   108  <*>{eof}                        return rune2class(lex.RuneEOF)
   109  
   110  "!="                            return NEQ
   111  "%:"                            return '#'
   112  "%="                            return MODASSIGN
   113  "%>"                            return '}'
   114  "&&"                            return ANDAND
   115  "&="                            return ANDASSIGN
   116  "*="                            return MULASSIGN
   117  "++"                            return INC
   118  "+="                            return ADDASSIGN
   119  "--"                            return DEC
   120  "-="                            return SUBASSIGN
   121  "->"                            return ARROW
   122  "..."                           return DDD
   123  "/="                            return DIVASSIGN
   124  ":>"                            return ']'
   125  "<%"                            return '{'
   126  "<:"                            return '['
   127  "<<"                            return LSH
   128  "<<="                           return LSHASSIGN
   129  "<="                            return LEQ
   130  "=="                            return EQ
   131  ">="                            return GEQ
   132  ">>"                            return RSH
   133  ">>="                           return RSHASSIGN
   134  "^="                            return XORASSIGN
   135  "|="                            return ORASSIGN
   136  "||"                            return OROR
   137  
   138  "##"                            |
   139  "#%:"                           |
   140  "%:#"                           |
   141  "%:%:"                          return PPPASTE
   142  
   143  
   144  <DIRECTIVE>"define"             l.pop(); return PPDEFINE
   145  <DIRECTIVE>"elif"               l.pop(); return PPELIF
   146  <DIRECTIVE>"else"               l.pop(); return PPELSE
   147  <DIRECTIVE>"endif"              l.pop(); return PPENDIF
   148  <DIRECTIVE>"error"              l.pop(); return PPERROR
   149  <DIRECTIVE>"if"                 l.pop(); return PPIF
   150  <DIRECTIVE>"ifdef"              l.pop(); return PPIFDEF
   151  <DIRECTIVE>"ifndef"             l.pop(); return PPIFNDEF
   152  <DIRECTIVE>"include"            l.pop(); return PPINCLUDE
   153  <DIRECTIVE>"include_next"       l.pop(); return PPINCLUDE_NEXT
   154  <DIRECTIVE>"line"               l.pop(); return PPLINE
   155  <DIRECTIVE>"pragma"             l.pop(); return PPPRAGMA
   156  <DIRECTIVE>"undef"              l.pop(); return PPUNDEF
   157  
   158  <HEADER>{header-name}           l.sc = scINITIAL
   159                                  return PPHEADER_NAME
   160  
   161  L{character-constant}           return LONGCHARCONST
   162  L{string-literal}               return LONGSTRINGLITERAL
   163  {character-constant}            return CHARCONST
   164  {identifier}                    return IDENTIFIER
   165  <DEFINE>{identifier}"("         return IDENTIFIER_LPAREN
   166  {integer-constant}              return INTCONST
   167  {floating-constant}             return FLOATCONST
   168  {pp-number}                     return PPNUMBER
   169  {string-literal}                return STRINGLITERAL
   170  
   171  %%
   172          if c, ok := l.Abort(); ok {
   173                  return c
   174          }
   175          
   176          goto yyAction
   177  }