coan 4.2.4
lexicon.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004, 2006 Symbian Software Ltd.                        *
00003  *   All rights reserved.                                                  *
00004  *   Copyright (C) 2007-2011 Mike Kinghan, imk@strudl.org                  *
00005  *   All rights reserved.                                                  *
00006  *                                                                         *
00007  *   Contributed originally by Mike Kinghan, imk@strudl.org                *
00008  *                                                                         *
00009  *   Redistribution and use in source and binary forms, with or without    *
00010  *   modification, are permitted provided that the following conditions    *
00011  *   are met:                                                              *
00012  *                                                                         *
00013  *   Redistributions of source code must retain the above copyright        *
00014  *   notice, this list of conditions and the following disclaimer.         *
00015  *                                                                         *
00016  *   Redistributions in binary form must reproduce the above copyright     *
00017  *   notice, this list of conditions and the following disclaimer in the   *
00018  *   documentation and/or other materials provided with the distribution.  *
00019  *                                                                         *
00020  *   Neither the name of Symbian Software Ltd. nor the names of its        *
00021  *   contributors may be used to endorse or promote products derived from  *
00022  *   this software without specific prior written permission.              *
00023  *                                                                         *
00024  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   *
00025  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     *
00026  *   LIMITED TO, THE IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS    *
00027  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE        *
00028  *   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,   *
00029  *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  *
00030  *   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
00031  *   OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    *
00032  *   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
00033  *   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
00034  *   THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH  *
00035  *   DAMAGE.                                                               *
00036  *                                                                         *
00037  **************************************************************************/
00038 
00039 #include "lexicon.h"
00040 #include "chew.h"
00041 #include "symbol.h"
00042 
00050 
00051 /* Helpers */
00052 
00064 static int
00065 text_match(char const **ptxt, char const *match, size_t nbytes)
00066 {
00067     int cmp;
00068     char const *txt = *ptxt;
00069     if (!nbytes) {
00070         nbytes = strlen(match);
00071     }
00072     for ( ; --nbytes && *txt && *match && *txt == *match; txt = chew_continuation(++txt),++match) {}
00073     if (*txt < *match) {
00074         cmp = -1;
00075     } else if (*txt > *match) {
00076         cmp = 1;
00077     } else {
00078         cmp = 0;
00079         *ptxt = chew_continuation(++txt);
00080     }
00081     return cmp;
00082 }
00083 
00086 /* API*/
00087 
00096 bool
00097 match_keyword(char const **ptxt, char const *keyword)
00098 {
00099     char const *saved_txt = *ptxt;
00100     if (0 == text_match(ptxt,keyword,0) && !is_symbol_start_char(**ptxt)) {
00101         return true;
00102     } else {
00103         *ptxt = saved_txt;
00104         return false;
00105     }
00106 }
00107 
00108 bool
00109 match_op(char const ** ptxt, char const *op)
00110 {
00111     char const *txt = *ptxt;
00112     bool verdict = true;
00113     assert(op[1] == '\0' || op[2] =='\0');
00114     if (txt[0] != op[0]) {
00115         verdict = false;
00116     } else {
00117         if (op[1]) {
00118             txt = chew_continuation(++txt);
00119             if (txt[0] != op[1]) {
00120                 verdict = false;
00121             }
00122         } else if (txt[1] == op[0]) {
00123             verdict = false;
00124         }
00125     }
00126     if (verdict) {
00127         *ptxt = txt + 1;
00128     }
00129     return verdict;
00130 }
00131 
00132 /* EOF*/
00133 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines