Mercurial > octave
comparison src/lex.l @ 1497:48a0b289f1be
[project @ 1995-09-30 22:04:27 by jwe]
author | jwe |
---|---|
date | Sat, 30 Sep 1995 22:06:19 +0000 |
parents | 3e705c864019 |
children | f07d425a9b04 |
comparison
equal
deleted
inserted
replaced
1496:8bd5499ae4ef | 1497:48a0b289f1be |
---|---|
34 #include <cstring> | 34 #include <cstring> |
35 | 35 |
36 #include <strstream.h> | 36 #include <strstream.h> |
37 | 37 |
38 #include "SLStack.h" | 38 #include "SLStack.h" |
39 | |
40 // These would be alphabetical, but y.tab.h must be included before | |
41 // oct-gperf.h and y.tab.h must be included after token.h and the tree | |
42 // class declarations. We can't include y.tab.h in oct-gperf.h | |
43 // because it may not be protected to allow it to be included multiple | |
44 // times. | |
39 | 45 |
40 #include "error.h" | 46 #include "error.h" |
41 #include "input.h" | 47 #include "input.h" |
42 #include "lex.h" | 48 #include "lex.h" |
43 #include "octave.h" | 49 #include "octave.h" |
52 #include "tree-plot.h" | 58 #include "tree-plot.h" |
53 #include "user-prefs.h" | 59 #include "user-prefs.h" |
54 #include "utils.h" | 60 #include "utils.h" |
55 #include "variables.h" | 61 #include "variables.h" |
56 #include "y.tab.h" | 62 #include "y.tab.h" |
63 #include "oct-gperf.h" | |
57 | 64 |
58 // Stack to hold tokens so that we can delete them when the parser is | 65 // Stack to hold tokens so that we can delete them when the parser is |
59 // reset and avoid growing forever just because we are stashing some | 66 // reset and avoid growing forever just because we are stashing some |
60 // information. This has to appear before lex.h is included, because | 67 // information. This has to appear before lex.h is included, because |
61 // one of the macros defined there uses token_stack. | 68 // one of the macros defined there uses token_stack. |
929 } | 936 } |
930 | 937 |
931 int l = input_line_number; | 938 int l = input_line_number; |
932 int c = current_input_column; | 939 int c = current_input_column; |
933 | 940 |
934 // XXX FIXME XXX -- this has really become too large a list to search | 941 int len = strlen (s); |
935 // like this... | 942 |
936 | 943 const octave_kw *kw = octave_kw_lookup (s, len); |
937 int end_found = 0; | 944 |
938 if (strcmp ("break", s) == 0) | 945 if (kw) |
939 { | 946 { |
940 yylval.tok_val = new token (l, c); | 947 yylval.tok_val = 0; |
948 | |
949 switch (kw->kw_id) | |
950 { | |
951 case all_va_args_kw: | |
952 case break_kw: | |
953 case catch_kw: | |
954 case continue_kw: | |
955 case else_kw: | |
956 case elseif_kw: | |
957 case global_kw: | |
958 case return_kw: | |
959 case unwind_protect_cleanup_kw: | |
960 break; | |
961 | |
962 case end_kw: | |
963 yylval.tok_val = new token (token::simple_end, l, c); | |
964 break; | |
965 | |
966 case end_try_catch_kw: | |
967 yylval.tok_val = new token (token::try_catch_end, l, c); | |
968 break; | |
969 | |
970 case end_unwind_protect_kw: | |
971 yylval.tok_val = new token (token::unwind_protect_end, l, c); | |
972 break; | |
973 | |
974 case endfor_kw: | |
975 yylval.tok_val = new token (token::for_end, l, c); | |
976 break; | |
977 | |
978 case endfunction_kw: | |
979 yylval.tok_val = new token (token::function_end, l, c); | |
980 break; | |
981 | |
982 case endif_kw: | |
983 yylval.tok_val = new token (token::if_end, l, c); | |
984 break; | |
985 | |
986 case endwhile_kw: | |
987 yylval.tok_val = new token (token::while_end, l, c); | |
988 break; | |
989 | |
990 case for_kw: | |
991 case while_kw: | |
992 promptflag--; | |
993 looping++; | |
994 break; | |
995 | |
996 case if_kw: | |
997 iffing++; | |
998 promptflag--; | |
999 break; | |
1000 | |
1001 case try_kw: | |
1002 case unwind_protect_kw: | |
1003 promptflag--; | |
1004 break; | |
1005 | |
1006 case gplot_kw: | |
1007 plotting = 1; | |
1008 yylval.tok_val = new token (token::two_dee, l, c); | |
1009 break; | |
1010 | |
1011 case gsplot_kw: | |
1012 plotting = 1; | |
1013 yylval.tok_val = new token (token::three_dee, l, c); | |
1014 break; | |
1015 | |
1016 case replot_kw: | |
1017 plotting = 1; | |
1018 yylval.tok_val = new token (token::replot, l, c); | |
1019 break; | |
1020 | |
1021 case function_kw: | |
1022 if (defining_func) | |
1023 { | |
1024 error ("function keyword invalid within a function body"); | |
1025 | |
1026 if ((reading_fcn_file || reading_script_file) | |
1027 && curr_fcn_file_name) | |
1028 error ("defining new function near line %d of file `%s.m'", | |
1029 input_line_number, curr_fcn_file_name); | |
1030 else | |
1031 error ("defining new function near line %d", input_line_number); | |
1032 | |
1033 return LEXICAL_ERROR; | |
1034 } | |
1035 else | |
1036 { | |
1037 tmp_local_sym_tab = new symbol_table (); | |
1038 curr_sym_tab = tmp_local_sym_tab; | |
1039 defining_func = 1; | |
1040 promptflag--; | |
1041 beginning_of_function = 1; | |
1042 if (! (reading_fcn_file || reading_script_file)) | |
1043 input_line_number = 1; | |
1044 } | |
1045 break; | |
1046 | |
1047 default: | |
1048 panic_impossible (); | |
1049 } | |
1050 | |
1051 if (! yylval.tok_val) | |
1052 yylval.tok_val = new token (l, c); | |
1053 | |
941 token_stack.push (yylval.tok_val); | 1054 token_stack.push (yylval.tok_val); |
942 return BREAK; | 1055 |
943 } | 1056 return kw->tok; |
944 else if (strcmp ("continue", s) == 0) | 1057 } |
945 { | |
946 yylval.tok_val = new token (l, c); | |
947 token_stack.push (yylval.tok_val); | |
948 return CONTINUE; | |
949 } | |
950 else if (strcmp ("else", s) == 0) | |
951 { | |
952 yylval.tok_val = new token (l, c); | |
953 token_stack.push (yylval.tok_val); | |
954 return ELSE; | |
955 } | |
956 else if (strcmp ("elseif", s) == 0) | |
957 { | |
958 yylval.tok_val = new token (l, c); | |
959 token_stack.push (yylval.tok_val); | |
960 return ELSEIF; | |
961 } | |
962 else if (strcmp ("end", s) == 0) | |
963 { | |
964 end_found = 1; | |
965 yylval.tok_val = new token (token::simple_end, l, c); | |
966 token_stack.push (yylval.tok_val); | |
967 } | |
968 else if (strcmp ("endfor", s) == 0) | |
969 { | |
970 end_found = 1; | |
971 yylval.tok_val = new token (token::for_end, l, c); | |
972 token_stack.push (yylval.tok_val); | |
973 } | |
974 else if (strcmp ("endfunction", s) == 0) | |
975 { | |
976 end_found = 1; | |
977 yylval.tok_val = new token (token::function_end, l, c); | |
978 token_stack.push (yylval.tok_val); | |
979 } | |
980 else if (strcmp ("endif", s) == 0) | |
981 { | |
982 end_found = 1; | |
983 yylval.tok_val = new token (token::if_end, l, c); | |
984 token_stack.push (yylval.tok_val); | |
985 } | |
986 else if (strcmp ("endwhile", s) == 0) | |
987 { | |
988 end_found = 1; | |
989 yylval.tok_val = new token (token::while_end, l, c); | |
990 token_stack.push (yylval.tok_val); | |
991 } | |
992 else if (strcmp ("for", s) == 0) | |
993 { | |
994 promptflag--; | |
995 looping++; | |
996 yylval.tok_val = new token (l, c); | |
997 token_stack.push (yylval.tok_val); | |
998 return FOR; | |
999 } | |
1000 else if (strcmp ("function", s) == 0) | |
1001 { | |
1002 if (defining_func) | |
1003 { | |
1004 error ("function keyword invalid within a function body"); | |
1005 | |
1006 if ((reading_fcn_file || reading_script_file) | |
1007 && curr_fcn_file_name) | |
1008 error ("defining new function near line %d of file `%s.m'", | |
1009 input_line_number, curr_fcn_file_name); | |
1010 else | |
1011 error ("defining new function near line %d", input_line_number); | |
1012 | |
1013 return LEXICAL_ERROR; | |
1014 } | |
1015 else | |
1016 { | |
1017 tmp_local_sym_tab = new symbol_table (); | |
1018 curr_sym_tab = tmp_local_sym_tab; | |
1019 defining_func = 1; | |
1020 promptflag--; | |
1021 beginning_of_function = 1; | |
1022 if (! (reading_fcn_file || reading_script_file)) | |
1023 input_line_number = 1; | |
1024 return FCN; | |
1025 } | |
1026 } | |
1027 else if (strcmp ("global", s) == 0) | |
1028 { | |
1029 yylval.tok_val = new token (l, c); | |
1030 token_stack.push (yylval.tok_val); | |
1031 return GLOBAL; | |
1032 } | |
1033 else if (strcmp ("gplot", s) == 0) | |
1034 { | |
1035 plotting = 1; | |
1036 yylval.tok_val = new token (token::two_dee, l, c); | |
1037 token_stack.push (yylval.tok_val); | |
1038 return PLOT; | |
1039 } | |
1040 else if (strcmp ("gsplot", s) == 0) | |
1041 { | |
1042 plotting = 1; | |
1043 yylval.tok_val = new token (token::three_dee, l, c); | |
1044 token_stack.push (yylval.tok_val); | |
1045 return PLOT; | |
1046 } | |
1047 else if (strcmp ("replot", s) == 0) | |
1048 { | |
1049 plotting = 1; | |
1050 yylval.tok_val = new token (token::replot, l, c); | |
1051 token_stack.push (yylval.tok_val); | |
1052 return PLOT; | |
1053 } | |
1054 else if (strcmp ("if", s) == 0) | |
1055 { | |
1056 iffing++; | |
1057 promptflag--; | |
1058 yylval.tok_val = new token (l, c); | |
1059 token_stack.push (yylval.tok_val); | |
1060 return IF; | |
1061 } | |
1062 else if (strcmp ("return", s) == 0) | |
1063 { | |
1064 yylval.tok_val = new token (l, c); | |
1065 token_stack.push (yylval.tok_val); | |
1066 return FUNC_RET; | |
1067 } | |
1068 else if (strcmp ("while", s) == 0) | |
1069 { | |
1070 promptflag--; | |
1071 looping++; | |
1072 yylval.tok_val = new token (l, c); | |
1073 token_stack.push (yylval.tok_val); | |
1074 return WHILE; | |
1075 } | |
1076 else if (strcmp ("unwind_protect", s) == 0) | |
1077 { | |
1078 promptflag--; | |
1079 yylval.tok_val = new token (l, c); | |
1080 token_stack.push (yylval.tok_val); | |
1081 return UNWIND; | |
1082 } | |
1083 else if (strcmp ("unwind_protect_cleanup", s) == 0) | |
1084 { | |
1085 yylval.tok_val = new token (l, c); | |
1086 token_stack.push (yylval.tok_val); | |
1087 return CLEANUP; | |
1088 } | |
1089 else if (strcmp ("end_unwind_protect", s) == 0) | |
1090 { | |
1091 end_found = 1; | |
1092 yylval.tok_val = new token (token::unwind_protect_end, l, c); | |
1093 token_stack.push (yylval.tok_val); | |
1094 } | |
1095 else if (strcmp ("try", s) == 0) | |
1096 { | |
1097 promptflag--; | |
1098 yylval.tok_val = new token (l, c); | |
1099 token_stack.push (yylval.tok_val); | |
1100 return TRY; | |
1101 } | |
1102 else if (strcmp ("catch", s) == 0) | |
1103 { | |
1104 yylval.tok_val = new token (l, c); | |
1105 token_stack.push (yylval.tok_val); | |
1106 return CATCH; | |
1107 } | |
1108 else if (strcmp ("end_try_catch", s) == 0) | |
1109 { | |
1110 end_found = 1; | |
1111 yylval.tok_val = new token (token::try_catch_end, l, c); | |
1112 token_stack.push (yylval.tok_val); | |
1113 } | |
1114 else if (strcmp ("all_va_args", s) == 0) | |
1115 { | |
1116 yylval.tok_val = new token (l, c); | |
1117 token_stack.push (yylval.tok_val); | |
1118 return ALL_VA_ARGS; | |
1119 } | |
1120 | |
1121 if (end_found) | |
1122 return END; | |
1123 | 1058 |
1124 return 0; | 1059 return 0; |
1125 } | 1060 } |
1126 | 1061 |
1127 // Try to find an identifier. All binding to global or builtin | 1062 // Try to find an identifier. All binding to global or builtin |