Pol  Revision:3cfda13
compctx.cpp
Go to the documentation of this file.
1 
7 #include "compctx.h"
8 
9 #include <cstring>
10 #include <ctype.h>
11 
12 #include <format/format.h>
13 
14 namespace Pol
15 {
16 namespace Bscript
17 {
18 namespace
19 {
23 int eatToEndOfLine( CompilerContext& ctx )
24 {
25  const char* t = ctx.s;
26 
27  while ( *t && ( *t != '\r' ) && ( *t != '\n' ) )
28  t++;
29 
30  ctx.s = t;
31  return 0;
32 }
33 
39 int eatToCommentEnd( CompilerContext& ctx )
40 {
41  CompilerContext tmp( ctx );
42 
43  while ( tmp.s[0] )
44  {
45  if ( strncmp( tmp.s, "*/", 2 ) == 0 )
46  {
47  tmp.s += 2;
48  ctx = tmp;
49  return 0;
50  }
51  else if ( strncmp( tmp.s, "/*", 2 ) == 0 ) // nested comment
52  {
53  tmp.s += 2;
54  int res = eatToCommentEnd( tmp );
55  if ( res )
56  return res;
57  }
58  else if ( strncmp( tmp.s, "//", 2 ) == 0 ) // nested eol-comment
59  {
60  int res = eatToEndOfLine( tmp );
61  if ( res )
62  return res;
63  }
64  if ( tmp.s[0] == '\n' )
65  ++tmp.line;
66  tmp.s++;
67  }
68  return -1;
69 }
70 } // namespace
71 
73  : s( NULL ), line( 1 ), filename( "" ), s_begin( NULL ), dbg_filenum( 0 )
74 {
75 }
76 
77 CompilerContext::CompilerContext( const std::string& filename, int dbg_filenum, const char* s )
78  : s( s ), line( 1 ), filename( filename ), s_begin( s ), dbg_filenum( dbg_filenum )
79 {
80 }
81 
83  : s( ctx.s ),
84  line( ctx.line ),
85  filename( ctx.filename ),
86  s_begin( ctx.s_begin ),
88 {
89 }
90 
92 {
93  filename = rhs.filename;
94  s = rhs.s;
95  line = rhs.line;
96  s_begin = rhs.s_begin;
98 
99  return *this;
100 }
101 
106 {
107  while ( isspace( s[0] ) )
108  {
109  if ( s[0] == '\n' )
110  ++line;
111  s++;
112  }
113 }
114 
116 {
117  CompilerContext tctx( *this );
118  for ( ;; )
119  {
120  while ( tctx.s[0] && isspace( tctx.s[0] ) )
121  {
122  // FIXME: if (tctx.s[0] == '\t')
123  // FIXME: contains_tabs = true;
124  if ( tctx.s[0] == '\n' )
125  ++tctx.line;
126  tctx.s++; // wow neat, already take care of newlines
127  }
128 
129  if ( !tctx.s[0] )
130  {
131  ( *this ) = tctx;
132  return 1;
133  }
134 
135  // look for comments
136  if ( strncmp( tctx.s, "/*", 2 ) == 0 ) // got a start of comment
137  {
138  int res;
139  tctx.s += 2;
140  res = eatToCommentEnd( tctx );
141  if ( res )
142  return res;
143  }
144  else if ( strncmp( tctx.s, "//", 2 ) == 0 ) // comment, one line only
145  {
146  int res;
147  res = eatToEndOfLine( tctx );
148  if ( res )
149  return res;
150  }
151  else
152  {
153  break;
154  }
155  }
156 
157  ( *this ) = tctx;
158 
159  return 0;
160 }
161 
162 void CompilerContext::printOn( std::ostream& os ) const
163 {
164  os << "File: " << filename << ", Line " << line << std::endl;
165 }
166 
167 void CompilerContext::printOn( fmt::Writer& writer ) const
168 {
169  writer << "File: " << filename << ", Line " << line << "\n";
170 }
171 
172 void CompilerContext::printOnShort( std::ostream& os ) const
173 {
174  os << filename << ", Line " << line << std::endl;
175 }
176 
177 void CompilerContext::printOnShort( fmt::Writer& writer ) const
178 {
179  writer << filename << ", Line " << line << "\n";
180 }
181 }
182 }
void printOn(std::ostream &os) const
Definition: compctx.cpp:162
CompilerContext & operator=(const CompilerContext &)
Definition: compctx.cpp:91
void printOnShort(std::ostream &os) const
Definition: compctx.cpp:172
Definition: berror.cpp:12