00001 /* 00002 Copyright (c) 2009, National Institute of Advanced Industrial Science 00003 and Technology. All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are 00007 met: 00008 00009 * Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 00012 * Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions and the following disclaimer in 00014 the documentation and/or other materials provided with the 00015 distribution. 00016 00017 * Neither the name of Fixstars Corporation nor the names of its 00018 contributors may be used to endorse or promote products derived 00019 from this software without specific prior written permission. 00020 00021 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00024 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00025 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00027 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00028 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00029 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00030 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00031 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 */ 00033 00034 #ifndef CFRONT_LOCATION_NODE_HPP 00035 #define CFRONT_LOCATION_NODE_HPP 00036 00037 #include <antlr/CommonAST.hpp> 00038 #include <string> 00039 #include <sstream> 00040 #include <vector> 00041 00042 namespace cfront { 00043 00048 struct Location { 00049 Location() : line(-1), column(-1) {} 00050 void initialize() { filePath.assign(""); line = -1; column = -1; } 00051 std::string filePath; 00052 int line; 00053 int column; 00054 }; 00055 00056 00057 class LocationNode; 00058 00059 typedef ANTLR_USE_NAMESPACE(antlr)ASTRefCount<LocationNode> RefLocationNode; 00060 00064 class LocationNode : public ANTLR_USE_NAMESPACE(antlr)CommonAST { 00065 public: 00066 00067 typedef std::vector<Location> Locations; 00068 00070 LocationNode(const LocationNode& other) : CommonAST(other), 00071 location_(other.location_) {} 00072 00074 LocationNode() : CommonAST() {} 00075 virtual ~LocationNode() {} 00076 00077 const Location& getLocation() const 00078 { 00079 return location_; 00080 } 00081 00082 const Locations& getOptLocations() const 00083 { 00084 return optLoc_; 00085 } 00086 00087 void setLocation(const Location& loc) 00088 { 00089 location_ = loc; 00090 } 00091 00092 void addOptLocation(const Location& loc) 00093 { 00094 optLoc_.push_back(loc); 00095 } 00096 00097 void addOptLocation(const RefLocationNode& other) 00098 { 00099 optLoc_.push_back(other->getLocation()); 00100 } 00101 00102 void setLocation(RefLocationNode other) 00103 { 00104 location_ = other->getLocation(); 00105 /* FIXME */ 00106 //std::copy(other->optPos_.begin(), other->optPos_.end(), optPos_); 00107 const Locations& ls = other->getOptLocations(); 00108 for (Locations::const_iterator i = ls.begin(); i != ls.end(); ++i) { 00109 optLoc_.push_back(*i); 00110 } 00111 } 00112 00113 virtual void initialize(int t, const ANTLR_USE_NAMESPACE(std)string& txt) 00114 { 00115 CommonAST::initialize(t, txt); 00116 location_.initialize(); 00117 } 00118 virtual void initialize(ANTLR_USE_NAMESPACE(antlr)RefToken t) 00119 { 00120 CommonAST::initialize(t); 00121 location_.filePath = t->getFilename(); 00122 location_.line = t->getLine(); 00123 location_.column = t->getColumn(); 00124 } 00125 virtual void initialize(RefLocationNode ast) 00126 { 00127 CommonAST::initialize(ANTLR_USE_NAMESPACE(antlr)RefAST(ast)); 00128 setLocation(ast); 00129 } 00130 00132 virtual ANTLR_USE_NAMESPACE(antlr)RefAST clone() 00133 { 00134 return ANTLR_USE_NAMESPACE(antlr)RefAST(new LocationNode(*this)); 00135 } 00136 static ANTLR_USE_NAMESPACE(antlr)RefAST factory() 00137 { 00138 return ANTLR_USE_NAMESPACE(antlr)RefAST(RefLocationNode(new LocationNode())); 00139 } 00140 00141 virtual ANTLR_USE_NAMESPACE(std)string toStringList() const 00142 { 00143 if (location_.line == -1 && location_.column == -1) { 00144 return CommonAST::toStringList(); 00145 } else { 00146 std::stringstream ss; 00147 ss << "[" << location_.filePath << ":" << location_.line << ":" << 00148 location_.column << "] " << CommonAST::toStringList(); 00149 return ss.str(); 00150 } 00151 } 00152 00153 00154 protected: 00155 Location location_; 00156 Locations optLoc_; 00157 }; 00158 00159 } // namespace cfront 00160 00161 #endif 00162 00163 /* 00164 * Local Variables: 00165 * mode: C++ 00166 * c-basic-offset: 4 00167 * indent-tabs-mode: nil 00168 * End: 00169 */