\doxysection{token.\+c}
\label{token_8c_source}\index{src/token.c@{src/token.c}}
\textbf{ Go to the documentation of this file.}
\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{preprocessor}{\#include\ "{}token.h"{}}}
\DoxyCodeLine{00002\ }
\DoxyCodeLine{00010\ OPD\ *opdtok(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *str);}
\DoxyCodeLine{00011\ }
\DoxyCodeLine{00015\ CERR\ cerr\_linetok[]\ =\ \{}
\DoxyCodeLine{00016\ \ \ \ \ \{\ 104,\ \textcolor{stringliteral}{"{}label\ length\ is\ too\ long"{}}\ \},}
\DoxyCodeLine{00017\ \ \ \ \ \{\ 105,\ \textcolor{stringliteral}{"{}no\ command\ in\ the\ line"{}}\ \},}
\DoxyCodeLine{00018\ \};}
\DoxyCodeLine{00019\ }
\DoxyCodeLine{00023\ \textcolor{keyword}{static}\ CERR\ cerr\_opdtok[]\ =\ \{}
\DoxyCodeLine{00024\ \ \ \ \ \{\ 117,\ \textcolor{stringliteral}{"{}operand\ too\ many\ in\ DC"{}}\ \},}
\DoxyCodeLine{00025\ \ \ \ \ \{\ 118,\ \textcolor{stringliteral}{"{}operand\ length\ too\ long"{}}\ \},}
\DoxyCodeLine{00026\ \ \ \ \ \{\ 121,\ \textcolor{stringliteral}{"{}cannot\ get\ operand\ token"{}}\ \},}
\DoxyCodeLine{00027\ \ \ \ \ \{\ 123,\ \textcolor{stringliteral}{"{}unclosed\ quote"{}}\ \},}
\DoxyCodeLine{00028\ \};}
\DoxyCodeLine{00029\ }
\DoxyCodeLine{00030\ OPD\ *opdtok(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *str)}
\DoxyCodeLine{00031\ \{}
\DoxyCodeLine{00032\ \ \ \ \ OPD\ *opd\ =\ malloc\_chk(\textcolor{keyword}{sizeof}(OPD),\ \textcolor{stringliteral}{"{}opd"{}});}
\DoxyCodeLine{00033\ \ \ \ \ \textcolor{keywordtype}{char}\ *tok,\ *p,\ sepc\ =\ \textcolor{charliteral}{','};}
\DoxyCodeLine{00034\ \ \ \ \ \textcolor{keywordtype}{int}\ i\ =\ 0,\ cnt\_quote\ =\ 0;}
\DoxyCodeLine{00035\ \ \ \ \ \textcolor{keywordtype}{bool}\ quoting\ =\ \textcolor{keyword}{false};}
\DoxyCodeLine{00036\ }
\DoxyCodeLine{00037\ \ \ \ \ opd-\/>opdc\ =\ 0;}
\DoxyCodeLine{00038\ \ \ \ \ \textcolor{keywordflow}{if}(!str\ ||\ !str[0])\ \{}
\DoxyCodeLine{00039\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ opd;}
\DoxyCodeLine{00040\ \ \ \ \ \}}
\DoxyCodeLine{00041\ \ \ \ \ tok\ =\ p\ =\ strdup\_chk(str,\ \textcolor{stringliteral}{"{}opdtok.p"{}});}
\DoxyCodeLine{00042\ \ \ \ \ \textcolor{keywordflow}{do}\ \{}
\DoxyCodeLine{00043\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ オペランド数が多すぎる場合はエラー\ */}}
\DoxyCodeLine{00044\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(opd-\/>opdc\ >=\ OPDSIZE)\ \{}
\DoxyCodeLine{00045\ \ \ \ \ \ \ \ \ \ \ \ \ setcerr(117,\ \textcolor{stringliteral}{"{}"{}});\ \ \ \ \textcolor{comment}{/*\ operand\ is\ too\ many\ */}}
\DoxyCodeLine{00046\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00047\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00048\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ 先頭が等号(=)の場合\ */}}
\DoxyCodeLine{00049\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(p[i]\ ==\ \textcolor{charliteral}{'='})\ \{}
\DoxyCodeLine{00050\ \ \ \ \ \ \ \ \ \ \ \ \ i++;}
\DoxyCodeLine{00051\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00052\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ 「'」の場合\ */}}
\DoxyCodeLine{00053\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(p[i]\ ==\ \textcolor{charliteral}{'\(\backslash\)''})\ \{}
\DoxyCodeLine{00054\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ 「''」以外の場合はquote値を反転する\ */}}
\DoxyCodeLine{00055\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(p[i+1]\ !=\ \textcolor{charliteral}{'\(\backslash\)''}\ \&\&\ (quoting\ ==\ \textcolor{keyword}{false}\ ||\ p[i-\/1]\ !=\ \textcolor{charliteral}{'\(\backslash\)''}))\ \{}
\DoxyCodeLine{00056\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ quoting\ =\ !quoting;}
\DoxyCodeLine{00057\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00058\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ 「'」をカウントする。「''」の場合は1をカウント\ */}}
\DoxyCodeLine{00059\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(p[i+1]\ !=\ \textcolor{charliteral}{'\(\backslash\)''})\ \{}
\DoxyCodeLine{00060\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ cnt\_quote++;}
\DoxyCodeLine{00061\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00062\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00063\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(quoting\ ==\ \textcolor{keyword}{true})\ \{}
\DoxyCodeLine{00064\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ 「'」で開いたまま文字列が終了した場合\ */}}
\DoxyCodeLine{00065\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(!p[i])\ \{}
\DoxyCodeLine{00066\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ setcerr(123,\ str);\ \ \ \ \textcolor{comment}{/*\ unclosed\ quote\ */}}
\DoxyCodeLine{00067\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00068\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00069\ \ \ \ \ \ \ \ \ \ \ \ \ i++;}
\DoxyCodeLine{00070\ \ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00071\ \ \ \ \ \ \ \ \ \ \ \ \ i\ +=\ strcspn(p\ +\ i,\ \textcolor{stringliteral}{"{},\ "{}});}
\DoxyCodeLine{00072\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(i\ ==\ 0)\ \{}
\DoxyCodeLine{00073\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ setcerr(121,\ \textcolor{stringliteral}{"{}"{}});\ \ \ \ \textcolor{comment}{/*\ cannot\ get\ operand\ token\ */}}
\DoxyCodeLine{00074\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00075\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00076\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(i\ -\/\ cnt\_quote\ >\ OPDSIZE)\ \{}
\DoxyCodeLine{00077\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ setcerr(118,\ \textcolor{stringliteral}{"{}"{}});\ \ \ \ \textcolor{comment}{/*\ operand\ length\ too\ long\ */}}
\DoxyCodeLine{00078\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{00079\ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00080\ \ \ \ \ \ \ \ \ \ \ \ \ sepc\ =\ p[i];}
\DoxyCodeLine{00081\ \ \ \ \ \ \ \ \ \ \ \ \ opd-\/>opdv[(opd-\/>opdc)++]\ =\ strndup\_chk(p,\ i,\ \textcolor{stringliteral}{"{}opd-\/>opdv[]"{}});}
\DoxyCodeLine{00082\ \ \ \ \ \ \ \ \ \ \ \ \ p\ +=\ i\ +\ 1;}
\DoxyCodeLine{00083\ \ \ \ \ \ \ \ \ \ \ \ \ i\ =\ cnt\_quote\ =\ 0;}
\DoxyCodeLine{00084\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00085\ \ \ \ \ \}\ \textcolor{keywordflow}{while}(sepc\ ==\ \textcolor{charliteral}{','});}
\DoxyCodeLine{00086\ \ \ \ \ FREE(tok);}
\DoxyCodeLine{00087\ \ \ \ \ \textcolor{keywordflow}{return}\ opd;}
\DoxyCodeLine{00088\ \}}
\DoxyCodeLine{00089\ }
\DoxyCodeLine{00090\ \textcolor{comment}{/*\ assemble.hで定義された関数群\ */}}
\DoxyCodeLine{00091\ \textcolor{keywordtype}{void}\ addcerrlist\_tok()}
\DoxyCodeLine{00092\ \{}
\DoxyCodeLine{00093\ \ \ \ \ addcerrlist(ARRAYSIZE(cerr\_linetok),\ cerr\_linetok);}
\DoxyCodeLine{00094\ \ \ \ \ addcerrlist(ARRAYSIZE(cerr\_opdtok),\ cerr\_opdtok);}
\DoxyCodeLine{00095\ \}}
\DoxyCodeLine{00096\ }
\DoxyCodeLine{00097\ CMDLINE\ *linetok(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *line)}
\DoxyCodeLine{00098\ \{}
\DoxyCodeLine{00099\ \ \ \ \ \textcolor{keywordtype}{char}\ *tok\ =\ NULL,\ *p\ =\ NULL;}
\DoxyCodeLine{00100\ \ \ \ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{00101\ \ \ \ \ CMDLINE\ *cmdl\ =\ NULL;}
\DoxyCodeLine{00102\ }
\DoxyCodeLine{00103\ \ \ \ \ assert(line);}
\DoxyCodeLine{00104\ \ \ \ \ \textcolor{keywordflow}{if}(!line[0]\ ||\ line[0]\ ==\ \textcolor{charliteral}{'\(\backslash\)n'})\ \{}
\DoxyCodeLine{00105\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00106\ \ \ \ \ \}}
\DoxyCodeLine{00107\ \ \ \ \ tok\ =\ p\ =\ strdup\_chk(line,\ \textcolor{stringliteral}{"{}tok"{}});}
\DoxyCodeLine{00108\ \ \ \ \ \textcolor{comment}{/*\ コメントを削除\ */}}
\DoxyCodeLine{00109\ \ \ \ \ strip\_casl2\_comment(p);}
\DoxyCodeLine{00110\ \ \ \ \ \textcolor{comment}{/*\ 文字列末尾の改行と空白を削除\ */}}
\DoxyCodeLine{00111\ \ \ \ \ strip\_end(p);}
\DoxyCodeLine{00112\ \ \ \ \ \textcolor{comment}{/*\ 空行の場合、終了\ */}}
\DoxyCodeLine{00113\ \ \ \ \ \textcolor{keywordflow}{if}(!p[0])\ \{}
\DoxyCodeLine{00114\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ linetokfin;}
\DoxyCodeLine{00115\ \ \ \ \ \}}
\DoxyCodeLine{00116\ \ \ \ \ cmdl\ =\ malloc\_chk(\textcolor{keyword}{sizeof}(CMDLINE),\ \textcolor{stringliteral}{"{}cmdl"{}});}
\DoxyCodeLine{00117\ }
\DoxyCodeLine{00118\ \ \ \ \ \textcolor{comment}{/*\ ラベルの取得\ */}}
\DoxyCodeLine{00119\ \ \ \ \ \textcolor{comment}{/*\ 行の先頭が空白またはタブの場合、ラベルは空\ */}}
\DoxyCodeLine{00120\ \ \ \ \ \textcolor{keywordflow}{if}((i\ =\ strcspn(p,\ \textcolor{stringliteral}{"{}\ \(\backslash\)t"{}}))\ ==\ 0)\ \{}
\DoxyCodeLine{00121\ \ \ \ \ \ \ \ \ cmdl-\/>label\ =\ strdup\_chk(\textcolor{stringliteral}{"{}"{}},\ \textcolor{stringliteral}{"{}cmdl-\/>label"{}});}
\DoxyCodeLine{00122\ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00123\ \ \ \ \ \ \ \ \ cmdl-\/>label\ =\ strndup\_chk(p,\ i,\ \textcolor{stringliteral}{"{}cmdl-\/>label"{}});}
\DoxyCodeLine{00124\ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ ラベルの文字列が長すぎる場合はエラー\ */}}
\DoxyCodeLine{00125\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(i\ >\ LABELSIZE)\ \{}
\DoxyCodeLine{00126\ \ \ \ \ \ \ \ \ \ \ \ \ setcerr(104,\ cmdl-\/>label);\ \ \ \ \textcolor{comment}{/*\ label\ length\ is\ too\ long\ */}}
\DoxyCodeLine{00127\ \ \ \ \ \ \ \ \ \ \ \ \ FREE(cmdl-\/>label);}
\DoxyCodeLine{00128\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ linetokfin;}
\DoxyCodeLine{00129\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00130\ \ \ \ \ \}}
\DoxyCodeLine{00131\ }
\DoxyCodeLine{00132\ \ \ \ \ \textcolor{comment}{/*\ 命令の取得\ */}}
\DoxyCodeLine{00133\ \ \ \ \ \textcolor{comment}{/*\ 文字列先頭をラベルの次の文字に移動\ */}}
\DoxyCodeLine{00134\ \ \ \ \ p\ +=\ i;}
\DoxyCodeLine{00135\ \ \ \ \ \textcolor{comment}{/*\ 文字列先頭を、ラベルと命令の間の空白の後ろに移動\ */}}
\DoxyCodeLine{00136\ \ \ \ \ p\ +=\ strspn(p,\ \textcolor{stringliteral}{"{}\ \(\backslash\)t"{}});}
\DoxyCodeLine{00137\ \ \ \ \ \textcolor{comment}{/*\ 命令がない場合は、終了\ */}}
\DoxyCodeLine{00138\ \ \ \ \ \textcolor{keywordflow}{if}(!p[0])\ \{}
\DoxyCodeLine{00139\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(cmdl-\/>label)\ \{\ \ \ \ \ \ \textcolor{comment}{/*\ ラベルが定義されていて命令がない場合はエラー\ */}}
\DoxyCodeLine{00140\ \ \ \ \ \ \ \ \ \ \ \ \ setcerr(105,\ \textcolor{stringliteral}{"{}"{}});\ \ \ \ \textcolor{comment}{/*\ no\ command\ in\ the\ line\ */}}
\DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ FREE(cmdl-\/>label);}
\DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ FREE(cmdl);}
\DoxyCodeLine{00144\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{goto}\ linetokfin;}
\DoxyCodeLine{00145\ \ \ \ \ \}}
\DoxyCodeLine{00146\ \ \ \ \ \textcolor{comment}{/*\ 命令取得の実行\ */}}
\DoxyCodeLine{00147\ \ \ \ \ i\ =\ strcspn(p,\ \textcolor{stringliteral}{"{}\ \(\backslash\)t"{}});}
\DoxyCodeLine{00148\ \ \ \ \ cmdl-\/>cmd\ =\ strndup\_chk(p,\ i,\ \textcolor{stringliteral}{"{}cmdl.cmd"{}});}
\DoxyCodeLine{00149\ }
\DoxyCodeLine{00150\ \ \ \ \ \textcolor{comment}{/*\ オペランドの取得\ */}}
\DoxyCodeLine{00151\ \ \ \ \ \textcolor{comment}{/*\ 文字列先頭を、命令の次の文字に移動\ */}}
\DoxyCodeLine{00152\ \ \ \ \ p\ +=\ i;}
\DoxyCodeLine{00153\ \ \ \ \ \textcolor{comment}{/*\ 文字列先頭を、命令とオペランドの間の空白の後ろに移動\ */}}
\DoxyCodeLine{00154\ \ \ \ \ p\ +=\ strspn(p,\ \textcolor{stringliteral}{"{}\ \(\backslash\)t"{}});}
\DoxyCodeLine{00155\ \ \ \ \ \textcolor{comment}{/*\ オペランド取得の実行\ */}}
\DoxyCodeLine{00156\ \ \ \ \ cmdl-\/>opd\ =\ opdtok(p);}
\DoxyCodeLine{00157\ linetokfin:}
\DoxyCodeLine{00158\ \ \ \ \ FREE(tok);}
\DoxyCodeLine{00159\ \ \ \ \ \textcolor{keywordflow}{return}\ cmdl;}
\DoxyCodeLine{00160\ \}}
\end{DoxyCode}