1 /*===========================================================================
2  *
3  *                            PUBLIC DOMAIN NOTICE
4  *               National Center for Biotechnology Information
5  *
6  *  This software/database is a "United States Government Work" under the
7  *  terms of the United States Copyright Act.  It was written as part of
8  *  the author's official duties as a United States Government employee and
9  *  thus cannot be copyrighted.  This software/database is freely available
10  *  to the public for use. The National Library of Medicine and the U.S.
11  *  Government have not placed any restriction on its use or reproduction.
12  *
13  *  Although all reasonable efforts have been taken to ensure the accuracy
14  *  and reliability of the software and data, the NLM and the U.S.
15  *  Government do not and cannot warrant the performance or results that
16  *  may be obtained by using this software or data. The NLM and the U.S.
17  *  Government disclaim all warranties, express or implied, including
18  *  warranties of performance, merchantability or fitness for any particular
19  *  purpose.
20  *
21  *  Please cite the author in any work or product based on this material.
22  *
23  * ===========================================================================
24  *
25  */
26 
27 #include "SchemaParser.hpp"
28 
29 #include <klib/text.h>
30 
31 #include <kfs/mmap.h>
32 
33 #include "SchemaScanner.hpp"
34 #include "ParseTree.hpp"
35 
36 using namespace ncbi::SchemaParser;
37 #define YYDEBUG 1
38 #include "schema-tokens.h"
39 
SchemaParser()40 SchemaParser :: SchemaParser ()
41 :   m_root ( 0 )
42 {
43 }
44 
~SchemaParser()45 SchemaParser :: ~SchemaParser ()
46 {
47     delete m_root;
48 }
49 
50 bool
ParseString(const char * p_input,bool p_debug)51 SchemaParser :: ParseString ( const char * p_input, bool p_debug )
52 {
53     SchemaScanner s ( p_input, string_size ( p_input ), false );
54     Schema_debug = p_debug;
55     delete m_root;
56     m_root = 0;
57     return Schema_parse ( & m_root, & m_errors, & s . GetScanBlock () ) == 0;
58 }
59 
60 bool
ParseFile(const struct KFile * p_file,const char * p_fileName)61 SchemaParser :: ParseFile ( const struct KFile * p_file, const char * p_fileName )
62 {
63     delete m_root;
64     m_root = 0;
65 
66     assert ( p_file != 0 );
67 
68     bool ret = false;
69     const KMMap *mm;
70     rc_t rc = KMMapMakeRead ( & mm, p_file );
71     if ( rc == 0 )
72     {
73         const char *addr;
74         rc = KMMapAddrRead ( mm, ( const void ** ) & addr );
75         if ( rc == 0 )
76         {
77             size_t size;
78             rc = KMMapSize ( mm, & size );
79             if ( rc == 0 )
80             {
81                 SchemaScanner s ( addr, size, false );
82                 s . GetScanBlock () . file_name = p_fileName == 0 ? "" : p_fileName;
83                 ret = Schema_parse ( & m_root, & m_errors, & s . GetScanBlock () ) == 0;
84             }
85         }
86 
87         KMMapRelease ( mm );
88     }
89 
90     return ret;
91 }
92 
93 ParseTree*
MoveParseTree()94 SchemaParser :: MoveParseTree ()
95 {
96     ParseTree* ret = m_root;
97     m_root = 0;
98     return ret;
99 }