packages feed

yi-0.6.2.2: src/Yi/Lexer/common.hsinc

-- -*- Haskell -*-

-- The include file for alex-generated syntax highlighters.  Because alex
-- declares its own types, any wrapper must have the highlighter in scope...
-- so it must be included.  Doubleplusyuck.

-- | Scan one token. Return (maybe) a token and a new state.
alexScanToken :: (AlexState HlState, AlexInput) -> Maybe (Tok Token, (AlexState HlState, AlexInput))
alexScanToken (AlexState state lookedOfs pos, inp@(_prevCh,str)) =
    let (scn,lookahead) = alexScanUser' state inp (stateToInit state)
        lookedOfs' = max lookedOfs (posnOfs pos +~ Size lookahead) in
    case scn of
      AlexEOF -> Nothing
      AlexError inp' ->
        let errorHint = take 10 $ alexCollectChar inp'
        in error $ "lexical error around " ++ errorHint
      AlexSkip  inp' len  ->
        let chunk = take (fromIntegral len) str
        in alexScanToken (AlexState state lookedOfs' (moveStr pos chunk), inp')
      AlexToken inp' len act ->
        let (state', tokValue) = act chunk state
            chunk = take (fromIntegral len) str
            newPos = moveStr pos chunk
        in Just (Tok tokValue (posnOfs newPos ~- posnOfs pos) pos, (AlexState state' lookedOfs' newPos, inp'))

alexScan' input (I# (sc))
  = alexScanUser' undefined input (I# (sc))

alexScanUser' user input (I# (sc))
  = case alex_scan_tkn' user input 0# input sc AlexNone of
      (AlexNone, input', lookahead) ->
        case alexGetChar input of
          Nothing -> (AlexEOF, lookahead)
          Just _  -> (AlexError input', lookahead)
      (AlexLastSkip input len, _, lookahead) -> (AlexSkip input len, lookahead)
      (AlexLastAcc k input len, _, lookahead) -> (AlexToken input len k, lookahead)


-- Same as alex_scan_tkn, but also return the length of lookahead.
alex_scan_tkn' user orig_input len input s last_acc =
  input `seq` -- strict in the input
  let new_acc = check_accs (alex_accept `quickIndex` (I# (s))) in
  new_acc `seq`
  case alexGetChar input of
     Nothing -> (new_acc, input, I# len)
     Just (c, new_input) ->
      let base       = alexIndexInt32OffAddr alex_base s
          (I# ord_c) = ord c
          offset     = (base +# ord_c)
          check      = alexIndexInt16OffAddr alex_check offset

          new_s      = if (offset >=# 0#) && (check ==# ord_c)
                       then alexIndexInt16OffAddr alex_table offset
                       else alexIndexInt16OffAddr alex_deflt s
          new_len    = len +# 1#
      in case new_s of
          -1# -> (new_acc, input, I# new_len)
          -- on an error, we want to keep the input *before* the
          -- character that failed, not after.
          -- (but still, we looked after)
          _ -> alex_scan_tkn' user orig_input new_len new_input new_s new_acc

  where
    check_accs [] = last_acc
    check_accs (AlexAcc a : _) = AlexLastAcc a input (I# len)
    check_accs (AlexAccSkip : _)  = AlexLastSkip  input (I# len)
    check_accs (AlexAccPred a pred : rest)
       | pred user orig_input (I# len) input
       = AlexLastAcc a input (I# len)
    check_accs (AlexAccSkipPred pred : rest)
       | pred user orig_input (I# len) input
       = AlexLastSkip input (I# len)
    check_accs (_ : rest) = check_accs rest

c = actionConst
m = actionAndModify
ms = actionStringAndModify
cs = actionStringConst