1 // Copyright 2017 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <string>
6 #include <vector>
7 
8 #include "parser.h"
9 
10 using std::string;
11 using std::vector;
12 
tokenize_string(const string & str)13 std::vector<string> tokenize_string(const string& str) {
14     std::vector<string> tokens;
15     string tok;
16 
17     for (auto& c : str) {
18         if (isalnum(c) || c == '_') {
19             tok += c;
20         } else {
21             if (!tok.empty())
22                 tokens.push_back(tok);
23             tok.clear();
24             if (ispunct(c))
25                 tokens.emplace_back(1, c);
26         }
27     }
28     if (!tok.empty())
29         tokens.push_back(tok);
30 
31     return tokens;
32 }
33 
operator +=(std::vector<string> & v1,const std::vector<string> & v2)34 std::vector<string>& operator+=(std::vector<string>& v1, const std::vector<string>& v2) {
35     v1.insert(v1.end(), v2.begin(), v2.end());
36     return v1;
37 }
38 
print_error(const char * what,const string & extra) const39 void FileCtx::print_error(const char* what, const string& extra) const {
40     if (line_end) {
41         fprintf(stderr, "error: %s : lines %d-%d : %s '%s' [near: %s]\n",
42                 file, line_start, line_end, what, extra.c_str(), last_token);
43     } else {
44         fprintf(stderr, "error: %s : line %d : %s '%s' [near: %s]\n",
45                 file, line_start, what, extra.c_str(), last_token);
46     }
47 }
48 
print_info(const char * what) const49 void FileCtx::print_info(const char* what) const {
50     fprintf(stderr, "%s : line %d : %s\n", file, line_start, what);
51 }
52 
53 std::string eof_str;
54 
curr()55 const string& TokenStream::curr() {
56     if (ix_ >= tokens_.size())
57         return eof_str;
58     return tokens_[ix_];
59 }
60 
next()61 const string& TokenStream::next() {
62     ix_ += 1u;
63     if (ix_ >= tokens_.size()) {
64         fc_.print_error("unexpected end of file", "");
65         return eof_str;
66     }
67     return tokens_[ix_];
68 }
69 
peek_next() const70 const string& TokenStream::peek_next() const {
71     auto n = ix_ + 1;
72     return (n >= tokens_.size()) ? eof_str : tokens_[n];
73 }
74 
filectx()75 const FileCtx& TokenStream::filectx() {
76     fc_.last_token = curr().c_str();
77     return fc_;
78 }
79