coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
parsed_line.h
Go to the documentation of this file.
1 #ifndef PARSED_LINE_H
2 #define PARSED_LINE_H
3 #pragma once
4 /***************************************************************************
5  * Copyright (C) 2013 Mike Kinghan, imk@burroingroingjoing.com *
6  * All rights reserved. *
7  * *
8  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
9  * *
10  * Redistribution and use in source and binary forms, with or without *
11  * modification, are permitted provided that the following conditions *
12  * are met: *
13  * *
14  * Redistributions of source code must retain the above copyright *
15  * notice, this list of conditions and the following disclaimer. *
16  * *
17  * Redistributions in binary form must reproduce the above copyright *
18  * notice, this list of conditions and the following disclaimer in the *
19  * documentation and/or other materials provided with the distribution. *
20  * *
21  * Neither the name of Mike Kinghan nor the names of its contributors *
22  * may be used to endorse or promote products derived from this software *
23  * without specific prior written permission. *
24  * *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
32  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
35  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
36  * DAMAGE. *
37  * *
38  **************************************************************************/
39 
43 #include "parse_buffer.h"
44 #include "directive_type.h"
45 
51 {
54 
56  explicit parsed_line(std::istream * in, std::ostream * out)
57  : _in(in),
58  _out(out){}
59 
60  ~parsed_line() override {}
61 
65  std::istream * file() {
66  return _in;
67  }
68 
70  unsigned num() const {
71  return _lineno;
72  }
73 
75  unsigned extensions() const {
76  return _extensions;
77  }
78 
80  void mark_keyword(size_t off, size_t len) {
81  _keyword_posn = off;
82  _keyword_len = len;
83  }
84 
88  bool get();
89 
91  void replace(std::string const & replacement) {
92  _text = replacement;
93  }
94 
113  template<directive_type DirectiveType>
114  void keyword_edit();
115 
117  void set_simplified(bool value = true) {
118  _simplified = value;
119  }
120 
122  bool is_simplified() const {
123  return _simplified;
124  }
125 
127  bool reportable() {
128  return _reportable;
129  }
130 
133  _dtype = dtype;
134  set_reportable();
135  }
136 
139  return _dtype;
140  }
141 
143  void set_dropping();
144 
146  bool dropping() const {
147  return _dropping;
148  }
149 
151  void output();
152 
154  void drop();
155 
157  unsigned & indent() {
158  return _indent;
159  }
160 
161 protected:
162 
164  size_t extension_pending(size_t off) const override {
165  if (at(off) == '\\') {
166  auto advance = eol(++off);
167  if (advance && overshoot(advance + off)) {
168  return ++advance;
169  }
170  }
171  return 0;
172  }
173 
177  size_t extend(size_t skip) override;
178 
180  size_t extend() override;
181 
191  void keyword_lop(std::string const & keyword);
192 
198  void keyword_swap(std::string const & directive);
199 
201  void write();
203  void write_commented_out();
205  void write_fast();
207  void write_slow();
216  void write(bool keep);
217 
221  void set_reportable();
222 
224  unsigned _extensions = 0;
226  unsigned _lineno = 0;
228  std::istream * _in;
230  std::ostream * _out;
232  size_t _keyword_posn = 0;
234  size_t _keyword_len = 0;
238  bool _reportable = false;
240  bool _dropping = false;
242  bool _simplified = false;
244  unsigned _drop_run_length = 0;
246  unsigned _indent = 0;
247 };
248 
249 #endif //EOF
void mark_keyword(size_t off, size_t len)
Record the offset and length of a directive keyword in the line.
Definition: parsed_line.h:80
bool is_simplified() const
Say whether the line has been simplified.
Definition: parsed_line.h:122
void write_slow()
Output the line slowely, when necessary.
void set_reportable()
Record whether the line is reportable for the operative command.
unsigned & indent()
Get a reference to the line's indentation amount.
Definition: parsed_line.h:157
void set_directive_type(directive_type dtype)
Set the directive type of the line.
Definition: parsed_line.h:132
std::istream * _in
The input stream from which this line is read.
Definition: parsed_line.h:228
parsed_line(std::istream *in, std::ostream *out)
Construct given pointers to input and output streams.
Definition: parsed_line.h:56
void keyword_lop(std::string const &keyword)
Convert the directive in the line into a another one that has no argument.
void write()
Output the line.
bool dropping() const
Are we dropping the line?
Definition: parsed_line.h:146
void keyword_swap(std::string const &directive)
Replace the directive keyword in the line with another one.
std::string _text
The data.
Definition: parse_buffer.h:209
Encapsulates a directive of a given type.
Definition: directive.h:141
directive_type _dtype
The directive type of this line.
Definition: parsed_line.h:236
bool overshoot(size_t off=0) const
Say whether an offset is out of tange.
Definition: parse_buffer.h:121
unsigned eol(size_t offset) const
Say whether there is a newline sequence at an offset.
Definition: parse_buffer.h:156
size_t _keyword_posn
Offset to directive keyword, if any.
Definition: parsed_line.h:232
void replace(std::string const &replacement)
Replace the line with another string.
Definition: parsed_line.h:91
bool _simplified
Has the line been simplified?
Definition: parsed_line.h:242
unsigned num() const
Get the greatest source line number spanned by this line.
Definition: parsed_line.h:70
void write_commented_out()
Output the line commented out.
directive_type
Symbolic constants denoting types of directives.
unsigned _indent
Amount by which the line is indented.
Definition: parsed_line.h:246
unsigned _extensions
The number of linefeeds embedded in the line.
Definition: parsed_line.h:224
struct parse_buffer is a polymorphic base for classes representing possibly extensible lines of parse...
Definition: parse_buffer.h:51
bool _dropping
Are we dropping this line.
Definition: parsed_line.h:240
std::istream * file()
Get a pointer to the input stream from which this parsed_line reads.
Definition: parsed_line.h:65
An unknown directive.
size_t extend() override
Try to read another line of input, returning the number of bytes read.
unsigned _drop_run_length
Count of contiguous lines that are dropped together.
Definition: parsed_line.h:244
size_t _keyword_len
Length of directive keyword, if any.
Definition: parsed_line.h:234
bool reportable()
Is the line reportable for the operative command.
Definition: parsed_line.h:127
unsigned extensions() const
Get the number of linefeeds embedded in this line.
Definition: parsed_line.h:75
bool _reportable
Is this line reportable?
Definition: parsed_line.h:238
size_t extension_pending(size_t off) const override
Say whether a line extension is pending at an offset in the line.
Definition: parsed_line.h:164
void set_simplified(bool value=true)
Classifiy the line as simplified or not.
Definition: parsed_line.h:117
directive_type get_directive_type() const
Get Set the directive type of the line.
Definition: parsed_line.h:138
void keyword_edit()
Replace the directive in the line.
std::ostream * _out
The output stream to which this line is written.
Definition: parsed_line.h:230
void write_fast()
Output the line fast, when possible.
void drop()
Drop the line.
unsigned _lineno
The greatest source line number spanned by this line.
Definition: parsed_line.h:226
void output()
Output the line.
void set_dropping()
Record whether we are dropping the line.
parse_buffer()=default
Default constuctor.
char at(size_t off) const
Get the character indexed by an offset. Not range-checked.
Definition: parse_buffer.h:126