Mercurial > octave-dspies
comparison src/lex.l @ 1823:5cdd59e7579a
[project @ 1996-02-01 12:45:05 by jwe]
author | jwe |
---|---|
date | Thu, 01 Feb 1996 12:45:05 +0000 |
parents | 3a9462b655f1 |
children | b14829582cc4 |
comparison
equal
deleted
inserted
replaced
1822:3a47ca3dd227 | 1823:5cdd59e7579a |
---|---|
30 #include <config.h> | 30 #include <config.h> |
31 #endif | 31 #endif |
32 | 32 |
33 #include <cctype> | 33 #include <cctype> |
34 #include <cstring> | 34 #include <cstring> |
35 | |
36 #include <string> | |
35 | 37 |
36 #include <strstream.h> | 38 #include <strstream.h> |
37 | 39 |
38 #include "SLStack.h" | 40 #include "SLStack.h" |
39 | 41 |
112 // file. | 114 // file. |
113 | 115 |
114 static void do_string_escapes (char *s); | 116 static void do_string_escapes (char *s); |
115 static void fixup_column_count (char *s); | 117 static void fixup_column_count (char *s); |
116 static void do_comma_insert_check (void); | 118 static void do_comma_insert_check (void); |
117 static int is_plot_keyword (char *s); | 119 static int is_plot_keyword (const string& s); |
118 static int is_keyword (char *s); | 120 static int is_keyword (const string& s); |
119 static char *plot_style_token (char *s); | 121 static string plot_style_token (const string& s); |
120 static symbol_record *lookup_identifier (char *s); | 122 static symbol_record *lookup_identifier (const string& s); |
121 static void grab_help_text (void); | 123 static void grab_help_text (void); |
122 static int match_any (char c, char *s); | 124 static int match_any (char c, char *s); |
123 static int next_token_is_bin_op (int spc_prev, char *yytext); | 125 static int next_token_is_bin_op (int spc_prev, char *yytext); |
124 static int next_token_is_postfix_unary_op (int spc_prev, char *yytext); | 126 static int next_token_is_postfix_unary_op (int spc_prev, char *yytext); |
125 static char *strip_trailing_whitespace (char *s); | 127 static string strip_trailing_whitespace (char *s); |
126 static void handle_number (char *yytext); | 128 static void handle_number (char *yytext); |
127 static int handle_string (char delim, int text_style = 0); | 129 static int handle_string (char delim, int text_style = 0); |
128 static int handle_close_brace (int spc_gobbled); | 130 static int handle_close_brace (int spc_gobbled); |
129 static int handle_identifier (char *tok, int spc_gobbled); | 131 static int handle_identifier (const string& tok, int spc_gobbled); |
130 static int have_continuation (int trailing_comments_ok = 1); | 132 static int have_continuation (int trailing_comments_ok = 1); |
131 static int have_ellipsis_continuation (int trailing_comments_ok = 1); | 133 static int have_ellipsis_continuation (int trailing_comments_ok = 1); |
132 static int eat_whitespace (void); | 134 static int eat_whitespace (void); |
133 static int eat_continuation (void); | 135 static int eat_continuation (void); |
134 | 136 |
188 current_input_column++; | 190 current_input_column++; |
189 return handle_string (yytext[0], 1); | 191 return handle_string (yytext[0], 1); |
190 } | 192 } |
191 | 193 |
192 <TEXT_FCN>[^ \t\n\;\,]*{S}* { | 194 <TEXT_FCN>[^ \t\n\;\,]*{S}* { |
193 char *tok = strip_trailing_whitespace (yytext); | 195 string tok = strip_trailing_whitespace (yytext); |
194 TOK_PUSH_AND_RETURN (tok, TEXT); | 196 TOK_PUSH_AND_RETURN (tok, TEXT); |
195 } | 197 } |
196 | 198 |
197 %{ | 199 %{ |
198 // For this and the next two rules, we're looking at ']', and we | 200 // For this and the next two rules, we're looking at ']', and we |
403 // Identifiers. Truncate the token at the first space or tab but | 405 // Identifiers. Truncate the token at the first space or tab but |
404 // don't write directly on yytext. | 406 // don't write directly on yytext. |
405 %} | 407 %} |
406 | 408 |
407 {IDENT}{S}* { | 409 {IDENT}{S}* { |
408 char *tok = strip_trailing_whitespace (yytext); | 410 string tok = strip_trailing_whitespace (yytext); |
409 int c = yytext[yyleng-1]; | 411 int c = yytext[yyleng-1]; |
410 int cont_is_spc = eat_continuation (); | 412 int cont_is_spc = eat_continuation (); |
411 int spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); | 413 int spc_gobbled = (cont_is_spc || c == ' ' || c == '\t'); |
412 return handle_identifier (tok, spc_gobbled); | 414 return handle_identifier (tok, spc_gobbled); |
413 } | 415 } |
857 } | 859 } |
858 | 860 |
859 // Check to see if a character string matches any of the possible line | 861 // Check to see if a character string matches any of the possible line |
860 // styles for plots. | 862 // styles for plots. |
861 | 863 |
862 static char * | 864 static string |
863 plot_style_token (char *s) | 865 plot_style_token (const string& s) |
864 { | 866 { |
867 string retval; | |
868 | |
865 static char *plot_styles[] = | 869 static char *plot_styles[] = |
866 { | 870 { |
867 "boxes", | 871 "boxes", |
868 "boxerrorbars", | 872 "boxerrorbars", |
869 "dots", | 873 "dots", |
877 }; | 881 }; |
878 | 882 |
879 char **tmp = plot_styles; | 883 char **tmp = plot_styles; |
880 while (*tmp) | 884 while (*tmp) |
881 { | 885 { |
882 if (almost_match (*tmp, s)) | 886 if (almost_match (*tmp, s.c_str ())) |
883 return *tmp; | 887 { |
888 retval = *tmp; | |
889 break; | |
890 } | |
884 | 891 |
885 tmp++; | 892 tmp++; |
886 } | 893 } |
887 | 894 |
888 return 0; | 895 return retval; |
889 } | 896 } |
890 | 897 |
891 // Check to see if a character string matches any one of the plot | 898 // Check to see if a character string matches any one of the plot |
892 // option keywords. Don't match abbreviations for clear, since that's | 899 // option keywords. Don't match abbreviations for clear, since that's |
893 // not a gnuplot keyword (users will probably only expect to be able | 900 // not a gnuplot keyword (users will probably only expect to be able |
894 // to abbreviate actual gnuplot keywords). | 901 // to abbreviate actual gnuplot keywords). |
895 | 902 |
896 static int | 903 static int |
897 is_plot_keyword (char *s) | 904 is_plot_keyword (const string& s) |
898 { | 905 { |
899 if (almost_match ("title", s)) | 906 const char *t = s.c_str (); |
907 if (almost_match ("title", t)) | |
900 { | 908 { |
901 return TITLE; | 909 return TITLE; |
902 } | 910 } |
903 else if (almost_match ("using", s)) | 911 else if (almost_match ("using", t)) |
904 { | 912 { |
905 in_plot_using = 1; | 913 in_plot_using = 1; |
906 return USING; | 914 return USING; |
907 } | 915 } |
908 else if (almost_match ("with", s)) | 916 else if (almost_match ("with", t)) |
909 { | 917 { |
910 in_plot_style = 1; | 918 in_plot_style = 1; |
911 return WITH; | 919 return WITH; |
912 } | 920 } |
913 else if (strcmp ("clear", s) == 0) | 921 else if (strcmp ("clear", t) == 0) |
914 { | 922 { |
915 return CLEAR; | 923 return CLEAR; |
916 } | 924 } |
917 else | 925 else |
918 { | 926 { |
921 } | 929 } |
922 | 930 |
923 // Handle keywords. Could probably be more efficient... | 931 // Handle keywords. Could probably be more efficient... |
924 | 932 |
925 static int | 933 static int |
926 is_keyword (char *s) | 934 is_keyword (const string& s) |
927 { | 935 { |
928 if (plotting && in_plot_style) | 936 if (plotting && in_plot_style) |
929 { | 937 { |
930 char *sty = plot_style_token (s); | 938 string sty = plot_style_token (s); |
931 if (sty) | 939 |
940 if (! sty.empty ()) | |
932 { | 941 { |
933 in_plot_style = 0; | 942 in_plot_style = 0; |
934 yylval.tok_val = new token (sty); | 943 yylval.tok_val = new token (sty); |
935 token_stack.push (yylval.tok_val); | 944 token_stack.push (yylval.tok_val); |
936 return STYLE; | 945 return STYLE; |
938 } | 947 } |
939 | 948 |
940 int l = input_line_number; | 949 int l = input_line_number; |
941 int c = current_input_column; | 950 int c = current_input_column; |
942 | 951 |
943 int len = strlen (s); | 952 int len = s.length (); |
944 | 953 |
945 const octave_kw *kw = octave_kw_lookup (s, len); | 954 const octave_kw *kw = octave_kw_lookup (s.c_str (), len); |
946 | 955 |
947 if (kw) | 956 if (kw) |
948 { | 957 { |
949 yylval.tok_val = 0; | 958 yylval.tok_val = 0; |
950 | 959 |
1064 | 1073 |
1065 // Try to find an identifier. All binding to global or builtin | 1074 // Try to find an identifier. All binding to global or builtin |
1066 // variables occurs when expressions are evaluated. | 1075 // variables occurs when expressions are evaluated. |
1067 | 1076 |
1068 static symbol_record * | 1077 static symbol_record * |
1069 lookup_identifier (char *name) | 1078 lookup_identifier (const string& name) |
1070 { | 1079 { |
1071 return curr_sym_tab->lookup (name, 1, 0); | 1080 return curr_sym_tab->lookup (name, 1, 0); |
1072 } | 1081 } |
1073 | 1082 |
1074 // Grab the help text from an function file. Always overwrites the | 1083 // Grab the help text from an function file. Always overwrites the |
1248 return bin_op; | 1257 return bin_op; |
1249 } | 1258 } |
1250 | 1259 |
1251 // Used to delete trailing white space from tokens. | 1260 // Used to delete trailing white space from tokens. |
1252 | 1261 |
1253 static char * | 1262 static string |
1254 strip_trailing_whitespace (char *s) | 1263 strip_trailing_whitespace (char *s) |
1255 { | 1264 { |
1256 static char *retval = 0; | 1265 string retval = s; |
1257 | 1266 |
1258 delete [] retval; | 1267 size_t pos = retval.find_first_of (" \t"); |
1259 | 1268 |
1260 retval = strsave (s); | 1269 if (pos != NPOS) |
1261 | 1270 retval.resize (pos); |
1262 char *t = strchr (retval, ' '); | |
1263 if (t) | |
1264 *t = '\0'; | |
1265 | |
1266 t = strchr (retval, '\t'); | |
1267 if (t) | |
1268 *t = '\0'; | |
1269 | 1271 |
1270 return retval; | 1272 return retval; |
1271 } | 1273 } |
1272 | 1274 |
1273 // Discard whitespace, including comments and continuations. | 1275 // Discard whitespace, including comments and continuations. |
1657 | 1659 |
1658 // Figure out exactly what kind of token to return when we have seen | 1660 // Figure out exactly what kind of token to return when we have seen |
1659 // an identifier. Handles keywords. | 1661 // an identifier. Handles keywords. |
1660 | 1662 |
1661 static int | 1663 static int |
1662 handle_identifier (char *tok, int spc_gobbled) | 1664 handle_identifier (const string& tok, int spc_gobbled) |
1663 { | 1665 { |
1664 // It is almost always an error for an identifier to be followed | 1666 // It is almost always an error for an identifier to be followed |
1665 // directly by another identifier. Special cases are handled below. | 1667 // directly by another identifier. Special cases are handled below. |
1666 | 1668 |
1667 cant_be_identifier = 1; | 1669 cant_be_identifier = 1; |
1722 | 1724 |
1723 if (is_text_function_name (tok)) | 1725 if (is_text_function_name (tok)) |
1724 { | 1726 { |
1725 BEGIN TEXT_FCN; | 1727 BEGIN TEXT_FCN; |
1726 | 1728 |
1727 if (strcmp (tok, "set") == 0) | 1729 if (tok == "set") |
1728 doing_set = 1; | 1730 doing_set = 1; |
1729 } | 1731 } |
1730 | 1732 |
1731 int c = yyinput (); | 1733 int c = yyinput (); |
1732 yyunput (c, yytext); | 1734 yyunput (c, yytext); |