1# -*- coding: utf-8 -*- 2# Copyright 2010-2018, Google Inc. 3# All rights reserved. 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: 8# 9# * Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# * Redistributions in binary form must reproduce the above 12# copyright notice, this list of conditions and the following disclaimer 13# in the documentation and/or other materials provided with the 14# distribution. 15# * Neither the name of Google Inc. nor the names of its 16# contributors may be used to endorse or promote products derived from 17# this software without specific prior written permission. 18# 19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31"""A script to generate a C++ header file for the POS conversion map. 32""" 33 34__author__ = "keni" 35 36import optparse 37import sys 38 39from build_tools import code_generator_util 40 41 42HEADER = b"""// Copyright 2009 Google Inc. All Rights Reserved. 43// Author: keni 44 45#ifndef MOZC_DICTIONARY_POS_MAP_H_ 46#define MOZC_DICTIONARY_POS_MAP_H_ 47 48// POS conversion rules 49const POSMap kPOSMap[] = { 50""" 51FOOTER = b"""}; 52 53#endif // MOZC_DICTIONARY_POS_MAP_H_ 54""" 55 56def ParseUserPos(user_pos_file): 57 with open(user_pos_file, 'rb') as stream: 58 stream = code_generator_util.SkipLineComment(stream) 59 stream = code_generator_util.ParseColumnStream(stream, num_column=2) 60 return dict((key, enum_value) for key, enum_value in stream) 61 62 63def GeneratePosMap(third_party_pos_map_file, user_pos_file): 64 user_pos_map = ParseUserPos(user_pos_file) 65 66 result = {} 67 with open(third_party_pos_map_file, 'rb') as stream: 68 stream = code_generator_util.SkipLineComment(stream) 69 for columns in code_generator_util.ParseColumnStream(stream, num_column=2): 70 third_party_pos_name, mozc_pos = (columns + [None])[:2] 71 if mozc_pos is not None: 72 mozc_pos = user_pos_map[mozc_pos] 73 74 if third_party_pos_name in result: 75 assert (result[third_party_pos_name] == mozc_pos) 76 continue 77 78 result[third_party_pos_name] = mozc_pos 79 80 # Create mozc_pos to mozc_pos map. 81 for key, value in user_pos_map.items(): 82 if key in result: 83 assert (result[key] == value) 84 continue 85 result[key] = value 86 87 return result 88 89 90def OutputPosMap(pos_map, output): 91 output.write(HEADER) 92 for key, value in sorted(pos_map.items()): 93 key = code_generator_util.ToCppStringLiteral(key) 94 if value is None: 95 # Invalid PosType. 96 value = ( 97 b'static_cast< ::mozc::user_dictionary::UserDictionary::PosType>(-1)') 98 else: 99 value = b'::mozc::user_dictionary::UserDictionary::' + value 100 output.write(b' { %s, %s },\n' % (key, value)) 101 output.write(FOOTER) 102 103 104def ParseOptions(): 105 parser = optparse.OptionParser() 106 # Input: user_pos.def, third_party_pos_map.def 107 # Output: pos_map.h 108 parser.add_option('--user_pos_file', dest='user_pos_file', 109 help='Path to user_pos.def') 110 parser.add_option('--third_party_pos_map_file', 111 dest='third_party_pos_map_file', 112 help='Path to third_party_pos_map.def') 113 parser.add_option('--output', dest='output', 114 help='Path to output pos_map.h') 115 return parser.parse_args()[0] 116 117 118def main(): 119 options = ParseOptions() 120 121 pos_map = GeneratePosMap(options.third_party_pos_map_file, 122 options.user_pos_file) 123 124 with open(options.output, 'wb') as stream: 125 OutputPosMap(pos_map, stream) 126 127 128if __name__ == '__main__': 129 main() 130