strusBase  0.17
programLexer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
9 #ifndef _STRUS_BASE_PROGRAM_LEXER_HPP_INCLUDED
10 #define _STRUS_BASE_PROGRAM_LEXER_HPP_INCLUDED
11 #include "strus/base/regex.hpp"
12 #include <utility>
13 #include <string>
14 #include <vector>
15 
16 namespace strus {
17 
19 class ErrorBufferInterface;
20 
28 {
29 public:
31  Type type() const {return m_type;} // lexem type
32  int id() const {return m_id;} // enumeration index in case of a token
33  const std::string& value() const {return m_value;} // lexem value
34 
36  :m_type(type_),m_id(-1),m_value(){}
38  :m_type(o.m_type),m_id(o.m_id),m_value(o.m_value){}
39  ProgramLexem( Type type_, int id_, const std::string& value_)
40  :m_type(type_),m_id(id_),m_value(value_){}
41  operator bool() const {return !end();}
42 
43  bool end() const {return m_type == Eof || m_type == Error;}
44  bool isToken( int id_) const {return m_type == Token && m_id == id_;}
45  bool isToken() const {return m_type == Token;}
46  bool isString() const {return m_type == SQString || m_type == DQString;}
47  bool isError() const {return m_type == Error;}
48  bool isEof() const {return m_type == Eof;}
49  int checkKeyword( int nn, ...) const;
50 
51 private:
52  Type m_type; // lexem type
53  int m_id; // enumeration index in case of a token
54  std::string m_value; // lexem value
55 };
56 
59 {
60 public:
67  ProgramLexer( const char* src, const char* eolncomment_, const char** tokens, const char** errtokens, ErrorBufferInterface* errhnd_);
68  ~ProgramLexer();
69 
71  const ProgramLexem& next();
73  const ProgramLexem& current() {return m_lexem;}
75  const ProgramLexem& rescanCurrent();
76 
78  bool consumeToken( int tokid)
79  {
80  if (current().isToken( tokid))
81  {
82  next();
83  return true;
84  }
85  return false;
86  }
87 
93  bool setOption( Option opt_, bool value);
94 
97  int lineno() const;
98 
100  const char* pos() const {return m_src;}
101 
103  std::size_t len() const {return m_end - m_src;}
104 
108  bool skipto( char const* pos);
109 
112  const char* nextpos();
113 
116  const char* currentpos();
117 
122  std::string currentLocationString( int posincr, int size, const char* marker) const;
123 
124 private:
125 #if __cplusplus >= 201103L
126  ProgramLexer( const ProgramLexer&) = delete;
127  void operator=( const ProgramLexer&) = delete;
128 #else
129  ProgramLexer( const ProgramLexer&){}
130  void operator=( const ProgramLexer&){}
131 #endif
132 
133 private:
134  ErrorBufferInterface* m_errhnd;
135  const char* m_eolncomment;
136  std::vector<strus::RegexSearch*> m_lexems;
137  std::vector<strus::RegexSearch*> m_errlexems;
138  const char* m_start;
139  const char* m_end;
140  char const* m_src;
141  char const* m_prevsrc;
142  ProgramLexem m_lexem;
143  int m_opt;
144 };
145 
146 } //namespace
147 #endif
148 
Class for a lexer used for implementing domain specific languages of strus.
Definition: programLexer.hpp:58
ProgramLexem(Type type_, int id_, const std::string &value_)
Definition: programLexer.hpp:39
ProgramLexem(const ProgramLexem &o)
Definition: programLexer.hpp:37
const char * pos() const
Get the current source pointer.
Definition: programLexer.hpp:100
int checkKeyword(int nn,...) const
Definition: programLexer.hpp:88
const ProgramLexem & rescanCurrent()
Rescan the current lexem (with different options)
int id() const
Definition: programLexer.hpp:32
bool setOption(Option opt_, bool value)
Switch lexer option on/off.
Definition: programLexer.hpp:30
const std::string & value() const
Definition: programLexer.hpp:33
const char * nextpos()
Get the start of the next token.
const ProgramLexem & next()
Get the next lexem.
bool isString() const
Definition: programLexer.hpp:46
std::string currentLocationString(int posincr, int size, const char *marker) const
Get an excerpt of the current source location starting from a position with a marker inserted...
Class for a lexem in a domain specific programming language with the following properties.
Definition: programLexer.hpp:27
Definition: programLexer.hpp:30
Definition: programLexer.hpp:30
int lineno() const
Get the current line number.
Interface for reporting and catching errors in modules.
Definition: errorBufferInterface.hpp:24
ProgramLexem(Type type_=Error)
Definition: programLexer.hpp:35
Definition: programLexer.hpp:30
bool isToken() const
Definition: programLexer.hpp:45
bool isError() const
Definition: programLexer.hpp:47
Type type() const
Definition: programLexer.hpp:31
Type
Definition: programLexer.hpp:30
bool isToken(int id_) const
Definition: programLexer.hpp:44
bool end() const
Definition: programLexer.hpp:43
const ProgramLexem & current()
Get the current lexem.
Definition: programLexer.hpp:73
bool isEof() const
Definition: programLexer.hpp:48
bool consumeToken(int tokid)
Skip to next and return true if the current lexem is a token with a defined id.
Definition: programLexer.hpp:78
bool skipto(char const *pos)
Skip to a defined position is the parsed source and scan the next token.
Option
Definition: programLexer.hpp:88
ProgramLexer(const char *src, const char *eolncomment_, const char **tokens, const char **errtokens, ErrorBufferInterface *errhnd_)
Constructor.
Definition: programLexer.hpp:30
std::size_t len() const
Get the rest length to parse.
Definition: programLexer.hpp:103
const char * currentpos()
Get the start of the current token.