872 |
|
|
873 |
#endif // not BISON_STACK_HH] |
#endif // not BISON_STACK_HH] |
874 |
dnl |
dnl |
875 |
@output location.hh |
@output position.hh |
876 |
b4_copyright([Location class for Bison C++ parsers], [2002, 2003])[ |
b4_copyright([Position class for Bison C++ parsers], [2002, 2003])[ |
877 |
|
|
878 |
#ifndef BISON_LOCATION_HH |
/** |
879 |
# define BISON_LOCATION_HH |
** \file position.hh |
880 |
|
** Define the Location class. |
881 |
|
*/ |
882 |
|
|
883 |
|
#ifndef BISON_POSITION_HH |
884 |
|
# define BISON_POSITION_HH |
885 |
|
|
886 |
# include <iostream> |
# include <iostream> |
887 |
# include <string> |
# include <string> |
888 |
|
|
889 |
namespace yy |
namespace yy |
890 |
{ |
{ |
891 |
|
/** \brief Abstract a Position. */ |
892 |
class Position |
class Position |
893 |
{ |
{ |
894 |
public: |
public: |
895 |
Position () |
/** \brief Initial column number. */ |
896 |
: filename (), line (1), column (0) |
static const unsigned int initial_column = 0; |
897 |
{} |
/** \brief Initial line number. */ |
898 |
|
static const unsigned int initial_line = 1; |
899 |
|
|
900 |
|
/** \name Ctor & dtor. |
901 |
|
** \{ */ |
902 |
|
public: |
903 |
|
/** \brief Construct a Position. */ |
904 |
|
Position () : |
905 |
|
filename (), |
906 |
|
line (initial_line), |
907 |
|
column (initial_column) |
908 |
|
{ |
909 |
|
} |
910 |
|
/** \} */ |
911 |
|
|
912 |
|
|
913 |
|
/** \name Line and Column related manipulators |
914 |
|
** \{ */ |
915 |
|
public: |
916 |
|
/** \brief (line related) Advance to the LINES next lines. */ |
917 |
|
inline void lines (int lines = 1) |
918 |
|
{ |
919 |
|
column = initial_column; |
920 |
|
line += lines; |
921 |
|
} |
922 |
|
|
923 |
|
/** \brief (column related) Advance to the COLUMNS next columns. */ |
924 |
|
inline void columns (int columns = 1) |
925 |
|
{ |
926 |
|
column += columns; |
927 |
|
} |
928 |
|
/** \} */ |
929 |
|
|
930 |
|
public: |
931 |
|
/** \brief File name to which this position refers. */ |
932 |
std::string filename; |
std::string filename; |
933 |
int line; |
/** \brief Current line number. */ |
934 |
int column; |
unsigned int line; |
935 |
|
/** \brief Current column number. */ |
936 |
|
unsigned int column; |
937 |
}; |
}; |
938 |
|
|
939 |
|
/** \brief Add and assign a Position. */ |
940 |
|
inline const Position& |
941 |
|
operator+= (Position& res, const int width) |
942 |
|
{ |
943 |
|
res.columns (width); |
944 |
|
return res; |
945 |
|
} |
946 |
|
|
947 |
|
/** \brief Add two Position objects. */ |
948 |
|
inline const Position |
949 |
|
operator+ (const Position& begin, const int width) |
950 |
|
{ |
951 |
|
Position res = begin; |
952 |
|
return res += width; |
953 |
|
} |
954 |
|
|
955 |
|
/** \brief Add and assign a Position. */ |
956 |
|
inline const Position& |
957 |
|
operator-= (Position& res, const int width) |
958 |
|
{ |
959 |
|
return res += -width; |
960 |
|
} |
961 |
|
|
962 |
|
/** \brief Add two Position objects. */ |
963 |
|
inline const Position |
964 |
|
operator- (const Position& begin, const int width) |
965 |
|
{ |
966 |
|
return begin + -width; |
967 |
|
} |
968 |
|
|
969 |
|
/** \brief Intercept output stream redirection. |
970 |
|
** \param ostr the destination output stream |
971 |
|
** \param pos a reference to the Position to redirect |
972 |
|
*/ |
973 |
inline std::ostream& |
inline std::ostream& |
974 |
operator<< (std::ostream& ostr, const Position& pos) |
operator<< (std::ostream& ostr, const Position& pos) |
975 |
{ |
{ |
976 |
if (pos.filename != "") |
if (pos.filename != "") |
977 |
ostr << pos.filename << ':'; |
ostr << pos.filename << ':'; |
978 |
ostr << pos.line << '.' << pos.column; |
return ostr << pos.line << '.' << pos.column; |
|
return ostr; |
|
979 |
} |
} |
980 |
|
|
981 |
inline Position |
} |
982 |
operator- (const Position& pos, int col) |
#endif // not BISON_POSITION_HH] |
983 |
{ |
@output location.hh |
984 |
Position res (pos); |
b4_copyright([Location class for Bison C++ parsers], [2002, 2003])[ |
985 |
res.column -= col; |
|
986 |
return res; |
/** |
987 |
} |
** \file location.hh |
988 |
|
** Define the Location class. |
989 |
|
*/ |
990 |
|
|
991 |
|
#ifndef BISON_LOCATION_HH |
992 |
|
# define BISON_LOCATION_HH |
993 |
|
|
994 |
|
# include <iostream> |
995 |
|
# include <string> |
996 |
|
# include "position.hh" |
997 |
|
|
998 |
|
namespace yy |
999 |
|
{ |
1000 |
|
|
1001 |
|
/** \brief Abstract a Location. */ |
1002 |
class Location |
class Location |
1003 |
{ |
{ |
1004 |
|
/** \name Ctor & dtor. |
1005 |
|
** \{ */ |
1006 |
|
public: |
1007 |
|
/** \brief Construct a Location. */ |
1008 |
|
Location (void) : |
1009 |
|
begin (), |
1010 |
|
end () |
1011 |
|
{ |
1012 |
|
} |
1013 |
|
/** \} */ |
1014 |
|
|
1015 |
|
|
1016 |
|
/** \name Line and Column related manipulators |
1017 |
|
** \{ */ |
1018 |
|
public: |
1019 |
|
/** \brief Reset initial location to final location. */ |
1020 |
|
inline void step (void) |
1021 |
|
{ |
1022 |
|
begin = end; |
1023 |
|
} |
1024 |
|
|
1025 |
|
/** \brief Extend the current location to the COLUMNS next columns. */ |
1026 |
|
inline void columns (unsigned columns = 1) |
1027 |
|
{ |
1028 |
|
end += columns; |
1029 |
|
} |
1030 |
|
|
1031 |
|
/** \brief Extend the current location to the LINES next lines. */ |
1032 |
|
inline void lines (unsigned lines = 1) |
1033 |
|
{ |
1034 |
|
end.lines (lines); |
1035 |
|
} |
1036 |
|
/** \} */ |
1037 |
|
|
1038 |
|
|
1039 |
public: |
public: |
1040 |
|
/** \brief Beginning of the located region. */ |
1041 |
Position begin; |
Position begin; |
1042 |
|
/** \brief End of the located region. */ |
1043 |
Position end; |
Position end; |
1044 |
}; |
}; |
1045 |
|
|
1046 |
/* Don't issue twice the line number when the location is on a single |
/** \brief Join two Location objects to create a Location. */ |
1047 |
line. */ |
inline const Location operator+ (const Location& begin, const Location& end) |
1048 |
|
{ |
1049 |
|
Location res = begin; |
1050 |
|
res.end = end.end; |
1051 |
|
return res; |
1052 |
|
} |
1053 |
|
|
1054 |
inline std::ostream& |
/** \brief Add two Location objects */ |
1055 |
operator<< (std::ostream& ostr, const Location& pos) |
inline const Location operator+ (const Location& begin, unsigned width) |
1056 |
|
{ |
1057 |
|
Location res = begin; |
1058 |
|
res.columns (width); |
1059 |
|
return res; |
1060 |
|
} |
1061 |
|
|
1062 |
|
/** \brief Add and assign a Location */ |
1063 |
|
inline Location &operator+= (Location& res, unsigned width) |
1064 |
|
{ |
1065 |
|
res.columns (width); |
1066 |
|
return res; |
1067 |
|
} |
1068 |
|
|
1069 |
|
/** \brief Intercept output stream redirection. |
1070 |
|
** \param ostr the destination output stream |
1071 |
|
** \param pos a reference to the Position to redirect |
1072 |
|
** |
1073 |
|
** Don't issue twice the line number when the location is on a single line. |
1074 |
|
*/ |
1075 |
|
inline std::ostream& operator<< (std::ostream& ostr, const Location& pos) |
1076 |
{ |
{ |
1077 |
ostr << pos.begin; |
ostr << pos.begin; |
1078 |
if (pos.begin.filename != pos.end.filename) |
if (pos.begin.filename != pos.end.filename) |