summaryrefslogtreecommitdiff
path: root/scan.h
blob: a780ed6e51c0e0e96ab27a221ee0d20e67d6b406 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * scan.h: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *  (c) 2007-2013 Jörg Wendel
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 *
 */

//***************************************************************************
// Includes
//***************************************************************************

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <common.h>

//***************************************************************************
// Token Scanner
//***************************************************************************

class Scan
{
   public:

      Scan(const char* buf, int np = yes)  { asprintf(&buffer, "%s", buf); reset(); allowNumPrefix = np; }
      ~Scan()                     { free(buffer); }

      void reset()                { p = buffer; isStr = no; }
      const char* all()           { return buffer; }
      const char* lastString()    { return _last; }
      const char* lastIdent()     { return _last; }
      int lastInt()               { return atoi(_last); }
      int isEmpty()               { return _last[0] == 0; }
      int isString()              { return isStr; }
      int isIdent()               { return !isStr && isIdent(*_last); }
      int isNum()                 { return !isStr && isNum(*_last); }
      int isLogical()             { return !isStr && isLogical(*_last); }
      int isOperator()            { return !isStr && isOperator(*_last); }
      int hasValue(const char* v) { return strcmp(Str::notNull(v), Str::notNull(_last)) == 0; }

      const char* next()          { return p; }

      int eat(int aANP = na);

   private:

      void skipWs()            { while (*p && isWhiteSpace(*p)) p++; }

      int isDelimiter(char c)  { return strchr(delimiters, c) != 0; }
      int isWhiteSpace(char c) { return strchr(whitespace, c) != 0; }
      int isIdent(char c)      { return isalpha(c); }
      int isNum(char c)        { return isdigit(c) || isNumPrefix(c); }
      int isOperator(char c)   { return strchr(operators, c) != 0; }
      int isLogical(char c)    { return strchr(logicalOps, c) != 0; }
      int isNumPrefix(char c)  { return strchr(numprefix, c) != 0; }

      // data

      int isStr;
      const char* p;
      char* buffer;
      char _last[1000+TB];
      int allowNumPrefix;

      static const char* delimiters;
      static const char* operators;
      static const char* logicalOps;
      static const char* whitespace;
      static const char* numprefix;
};